How can I use bsxfun to replace for-loop in this code?

조회 수: 6 (최근 30일)
Shi Shi
Shi Shi 2017년 5월 19일
댓글: Shi Shi 2017년 5월 23일
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
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.
Shi Shi
Shi Shi 2017년 5월 19일
Thanks for your answer. The result calculated by inner for-loop is just what I want. For each pair (i,j), where i<j, I want to calculate the average of sequence(i:j).

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume
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
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.
Shi Shi
Shi Shi 2017년 5월 23일
well, I understand now. Thanks very very much.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
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:
  1. if A and B are the same size(), fun(A,B) is called directly and size(A) = size(B) outputs are expected
  2. 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
  3. 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.
  댓글 수: 3
Shi Shi
Shi Shi 2017년 5월 19일
Thanks very much for your reply. When avg(i, i+1:5) is called, I hope the result would be the same as arrayfun(avg, repmat(i, length(i+1:5)), i+1:5), because the document says "Whenever a dimension of A or B is singleton (equal to one), bsxfun virtually replicates the array along that dimension to match the other array". But the result is not as I thought. Actually,I have no idea what happens here of my bsxfun.
Shi Shi
Shi Shi 2017년 5월 19일
Thanks very much. It works on small sample very well. I will try it on a large sample later, in size of million.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 循环及条件语句에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!