How can I use bsxfun to replace for-loop in this code?
6 次查看(过去 30 天)
显示 更早的评论
Dear all,
I want to use the function 'bsxfun' instead of inner for-loop. My code is as following. However, the result, matrix B, calculated by 'bsxfun' is error. I feel very confused with how 'bsxfun' works. Could anyone help me to understand the result and use 'bsxfun' correctly instead of inner for-loop?
Thanks very much!
sequence = [1,2,3,4,5];
avg = @(i, j) mean(sequence(i:j));
for i = 1:4
for j = i+1:5
A(i,j) = avg(i,j);
end
B(i, i+1:5) = bsxfun(avg, i, i+1:5);
end
2 个评论
Adam
2017-5-19
What is the end result you are looking for? You should state that clearly ahead of being focused entirely on using a specific function.
采纳的回答
Guillaume
2017-5-19
"I want to use the function 'bsxfun' instead of inner for-loop" Rather than fixating on a function that may not be a solution to your problem, tell us what you want to do. See XY problem.
Your avg function cannot be used with bsxfun. The documentation of bsxfun is very explicit: fun must support scalar expansion. There is no way to support scalar expansion with an expression that includes a colon. So bsxfun is not an option at all.
There might be some obscure way to generate your A array without a loop (some weird combination of cumsum, toeplitz, and some other functions maybe) but honestly, you're better off with your loop approach. It will be a lot clearer and most likely, just as fast.
7 个评论
Guillaume
2017-5-22
The reason you can't use your avg function with bsxfun and the reason it returns 3.5 are all down to the : (colon) operator.
As per the documentation of colon, "If you specify nonscalar arrays, then MATLAB interprets j:i:k as j(1):i(1):k(1)." So it interprets [3, 3] : [4, 5] simply as 3 : 4, whose mean is indeed 3.5.
In effect your function ignores all but the first element of any vector that it is passed. Hence, why it does not work with bsxfun.
更多回答(1 个)
Walter Roberson
2017-5-19
"Binary function to apply, specified as a function handle. fun must be a binary (two-input) element-wise function of the form C = fun(A,B) that accepts arrays A and B with compatible sizes. For more information, see Compatible Array Sizes for Basic Operations. fun must support scalar expansion, such that if A or B is a scalar, then C is the result of applying the scalar to every element in the other input array."
In practice what this means for bxfun(@fun, A, B) is:
- if A and B are the same size(), fun(A,B) is called directly and size(A) = size(B) outputs are expected
- if either A or B are scalars and the other is not, then fun(A,B) is called directly and size() of the non-scalar is the expected output size
- otherwise fun(A(:,K), B(K)) is called once for each column K in A with size(A,1)x1 expected output size
Your case matches the first of those, so avg(i, i+1:5) is going to be called -- but your avg code expects the second input to be a scalar rather than a vector.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!