how bsxfun works? the result is weird.

why the result of this statement
z1=bsxfun(@(y1,y2)(y1+[y2(2:end),24]),zeros(4,1),1:4)
is
z1 =
24 24 24 24
24 24 24 24
24 24 24 24
24 24 24 24

4 Comments

Adam
Adam on 17 Dec 2015
Edited: Adam on 17 Dec 2015
z1=bsxfun(@plus, zeros(4,1),[2:4 24])
is what I would do to achieve what I assume you are trying to do. To be honest I have never used bsxfun similar to how you are doing, but its result does surprise me a little. It is one of those functions I understand for the usages I have done myself, but not in its full wider usage.
John D'Errico
John D'Errico on 17 Dec 2015
Edited: John D'Errico on 17 Dec 2015
I fixed your question to read properly, as the matrix result appeared to show up as a vector.
Thank Adam and Jhon.
it is better to use bsxfun by placing the expression [y2(2:end),24] at the third input parameter than writing it into anonymous function.
however, this is just a simple example for what I want to do. If the expression is more complicated, for example, I want to use y2 twice in different forms like this
z2=bsxfun(@(y1,y2)(y1<[y2(2:end),0])&(y1>[1,y2(1:end-1)]),ones(4,1),0.5:3.5)
I think it should be equal to this
Y2=repmat(0.5:3.5,4,1);
z3=(ones(4,4)<[Y2(:,2:end),zeros(4,1)])&(ones(4,4)>[ones(4,1),Y2(:,1:end-1)])
But it is not. the result is
z2 =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
z3 =
0 1 0 0
0 1 0 0
0 1 0 0
0 1 0 0
so I wonder if there is some process that I do not understand in bsxfun.
You cannot index into the variable that is passed into bsxfun, expecting to use it as a vector.
The idea behind bsxfun (as you are using it) is that it takes a pair of vectors, then acts on every combination of elements. The function is applied to pairs of scalars here, as essentially an outer product. bsxfun does NOT apply the function to vectors! It applies the function to all combinations of scalar elements.
So you get bsxfun "confused" when you try this. Yes, it is non-obvious, but this is how bsxfun acts.

Sign in to comment.

 Accepted Answer

I don't see any bug in bsxfun and to me the result is entirely expected, although it would be better if the documentation gave more details on the internal implementation of bsxfun.
The best way to see what is going on is to replace the anonymous function by a normal function, so you can see what is passed by bsxfun:
function out = testfn(y1, y2)
out = y1 + [y2(2:end), 24];
end
bsxfun(@testfn, zeros(4,1), 24);
You'll see bsxfun passes to the function a column vector for y1 and a scalar for y2, so of course y2(2:end) is empty, so the function is just out = y1 + 24.
The documentation does says that the input function may receive either vectors of the same size or vectors and scalar that must be expanded to match the size of the vector.
The way bsxfun works makes sense to me, you have a matrix and a row vector as input, so bsxfun passes one scalar and the corresponding column of the matrix to your function and expect a column in return, relying on your function to do the expansion.

2 Comments

Basically, the way you're attempting to use bsxfun is misguided, you absolutely have no guarantee that the function you pass to bsxfun will receive a vector for either input arguments. One or the other can be scalar.
It turns out that (in 2014b), bsxfun will prioritise a column vector over a row vector, so whichever input has several rows will be passed as individual columns and the other as scalar, but there's no guarantee that this behaviour won't change in future versions as it's an implementation detail. So you certainly shouldn't rely on this.
Thanks a lot.
It is a good way to use a normal function to check the process of bsxfun.

Sign in to comment.

More Answers (1)

This appears to be a bug in BSXFUN, since the result of the operation
[y2(2:end),24]
is to create the vector [2 3 4 24]. So lets try a variation of your problem. First, as you posed it.
y1 = zeros(4,1);
y2 = 1:4;
z1=bsxfun(@(y1,y2)(y1+[y2(2:end),24]),y1,y2)
z1 =
24 24 24 24
24 24 24 24
24 24 24 24
24 24 24 24
And we see the array of all 24 elements as you found. Now, let me change it in a way that should not be relevant.
y2 = 1:4
y2 =
1 2 3 4
y2 = [y2(2:end),24]
y2 =
2 3 4 24
z1=bsxfun(@(y1,y2)(y1+y2),y1,y2)
z1 =
2 3 4 24
2 3 4 24
2 3 4 24
2 3 4 24
As you see, BSXFUN now gives the proper answer.
My guess is the parser is screwing something up here, that the answer is not in BSXFUN directly, since BSXFUN works properly when it is fed those vectors in simpler form.

4 Comments

I think it's just a limitation in that you're not supposed to index inside of fun() since that's not singleton. This should be rewritten as:
z1=bsxfun(@(y1,y2)(y1+y2),y1,[y2(2:end),24])
Yes, I accept that since y2 is an argument to bsxfun, that indexing into y2 will cause a problem. So clearly not a bug. Indexing into y2, INSIDE of the function handle that bsxfun gets will cause a problem.
No, there's no bug in bsxfun. y2 is scalar, so y2(2:end) is empty.
I said exactly that in my comment, recognizing there is no bug.

Sign in to comment.

Products

Tags

Asked:

on 17 Dec 2015

Edited:

on 17 Dec 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!