필터 지우기
필터 지우기

bsxfun seems to return too few values

조회 수: 1 (최근 30일)
Lachlan
Lachlan 2016년 7월 28일
편집: Stephen23 2016년 7월 28일
In Matlab 2016a, the following command behaves as I expect:
>> bsxfun (@(x,y)((y-x)), [1], [3, 4])
ans =
2 3
However, this very similar command returns a scalar, instead of a 1x2 matrix:
>> bsxfun (@(x,y)(min(y-x)), [1], [3, 4])
ans =
2
Am I right in assuming this is a bug in bsxfun? If so, how can I report it. If not, could someone please explain this behaviour?

채택된 답변

KSSV
KSSV 2016년 7월 28일
bsxfun (@(x,y)((y-x)), [1], [3, 4])
It's output is 2, 3. The minimum value of (2,3) is 2..The second line
bsxfun (@(x,y)(min(y-x)), [1], [3, 4])
is giving you minimum value of the output....
I am using MATLAB2015a and I got the same result as said.
  댓글 수: 2
Lachlan
Lachlan 2016년 7월 28일
Ahh... I see now what the documentation means by "element-by-element function". I assumed that FUNC is called element-by-element, but it means that the answers are only sensible if applying FUNC to an array returns the same value as if it is applied element-by-element.
Thanks for your help.
Stephen23
Stephen23 2016년 7월 28일
"I am using MATLAB2015a and I got the same result as said"
This does not answer the question "is this a bug?"

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

추가 답변 (1개)

Stephen23
Stephen23 2016년 7월 28일
편집: Stephen23 2016년 7월 28일
@Lachlan: this is a bug (or an undocumented "feature"), and you should report it. You can read how here:
When you read the bsxfun documentation it clearly states that "A binary element-wise function of the form C = fun(A,B) accepts arrays A and B of arbitrary, but equal size and returns output of the same size. Each element in the output array C is the result of an operation on the corresponding elements of A and B only. fun must also support scalar expansion, such that if A or B is a scalar, C is the result of applying the scalar to every element in the other input array."
The function you provided does not fulfill these conditions, because it returns scalar when provided with a vector and a scalar:
>> fun = @(x,y)min(y-x);
>> fun(1, [3, 4])
ans =
2
Note that scalar x was not expanded, as the documentation requires. So you are already using bsxfun outside of its documented functionality. The question is, what should bsxfun do when you provide an incorrect function that returns a scalar and not a vector?
By accepting this function and returning an undocumented output is at least misleading and at worst totally erroneous.
  댓글 수: 1
Lachlan
Lachlan 2016년 7월 28일
Thanks for the link. I've reported the issue, suggesting throwing an error for this input.

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by