According to any documentation page, it " determines if any element is a nonzero number". OK to me NaN is nonzero number so I don't understand the restult returned by any in this example. ALL result looks allright to me and somewhat contradicts with ANY
A = nan(1,3);
all(A) % expected
ans = logical
1
any(A) % Not expected
ans = logical
0
What do I miss? Any comment? Should I call it a bug?

8 件のコメント

Dyuman Joshi
Dyuman Joshi 2024 年 3 月 22 日
"NaN is nonzero number"
NaN is Not a Number, so I don't think it can be classified as a nonzero number.
Bruno Luong
Bruno Luong 2024 年 3 月 22 日
編集済み: Bruno Luong 2024 年 3 月 22 日
NaN is Not a Number
I would argue NaN is a very valid IEEE double number, and it is not a zero.
Bruno Luong
Bruno Luong 2024 年 3 月 24 日
編集済み: Bruno Luong 2024 年 3 月 24 日
Also just wonder: in the description document it is written
"B = any(A) tests along the first array dimension of A whose size does not equal 1"
I'm not sure what this sentence means, of course ANY and ALL work fine on scalar input, the size contain 1. Can someone shed the light?
Paul
Paul 2024 年 3 月 24 日
I think it's just a case of the doc page author(s) being imprecise and inconsistent.
If we ignore that statement, then the scalar case could either be interpreted as a vector case
isvector(1)
ans = logical
1
or a matrix case
ismatrix(1)
ans = logical
1
though if it were me I'd specifically call out the scalar case.
Other doc pages have the same deficiency, e.g. sum: "sum(A) returns the sum of the elements of A along the first array dimension whose size is greater than 1." BUT, sum does capture the scalar case in the Input Arguments section, though it would be more natural to include it at the top of the page under Description in my opinion.
In that same section for sum(), scalar in NOT shown as a valid input array, but the corresponding section of any DOES show scalar as a valid input array.
mean has the same arrangement as for sum(), i.e., capturing the scalar case under Input Arguments, though median does not, even though it works fine for scalars.
median(5)
ans = 5
Comparing the doc pages for sum() and mean() shows that the former explcitly lists "Data Types" and "Complex Number Support" for the input array, but the latter does not. Etc.
Bruno Luong
Bruno Luong 2024 年 3 月 25 日
編集済み: Bruno Luong 2024 年 3 月 25 日
https://www.mathworks.com/help/matlab/ref/sum.html "sum(A) returns the sum of the elements of A along the first array dimension whose size is greater than 1."
IMO this statement is not only imprescise but wrong
A = zeros(0,10);
S = sum(A);
size(S)
ans = 1x2
1 10
The size indicates it sums along the first dimension, which has length = 0; but NOT greater than 1. The first dimension whose size is greater than 1 is actually 2.
size(sum(A,1)) % match with sum(A)
ans = 1x2
1 10
size(sum(A,2)) % different than sum(A)
ans = 1x2
0 1
Paul
Paul 2024 年 3 月 25 日
Yes, I agree that statement on sum is incorrect for the empty case you've shown.
Also, although the doc page states: "If A is an empty 0-by-0 matrix, then sum(A) returns 0." it doesn't address the case of an empty matrix where one (or more?) dimensions are not zero. Similar situation with mean (and probably others).
Steven Lord
Steven Lord 2024 年 3 月 26 日
@Paul I think it's just a case of the doc page author(s) being imprecise and inconsistent.
No, it's a case where that first sentence is a statement of the general principle, not a complete description of the behavior. Note that the section of the Description where that appears continues on with four bullet points, and that the scalar case is handled by the first one since every scalar is a vector.
"If A is a vector, then B = any(A) returns logical 1 (true) if any of the elements of A is a nonzero number or is logical 1, and returns logical 0 (false) if all the elements are zero."
What that clause means is that if the array has any non-singleton dimensions, any operates as though you'd specified the first non-singleton dimension as the dim input argument. In the example below, not specifying a dimension is the same as specifying the third dimension because the first dimension of A is size 1 (singleton) and so is the second.
format compact
A = rand(1, 1, 3, 4) < 0.25;
d1 = any(A)
d1 = 1x1x1x4 logical array
d1(:,:,1,1) = 1 d1(:,:,1,2) = 0 d1(:,:,1,3) = 0 d1(:,:,1,4) = 1
d2 = any(A, 3)
d2 = 1x1x1x4 logical array
d2(:,:,1,1) = 1 d2(:,:,1,2) = 0 d2(:,:,1,3) = 0 d2(:,:,1,4) = 1
@Bruno Luong Yes, that statement in the sum documentation page is incorrect. I've reported it to the documentation staff.
@Paul "Also, although the doc page states: "If A is an empty 0-by-0 matrix, then sum(A) returns 0." it doesn't address the case of an empty matrix where one (or more?) dimensions are not zero."
0-by-0 empties are a special case for a number of functions, going back to the Olden Days when it was the only empty matrix you could create in MATLAB. Professor Carl de Boor even wrote a paper about it back in 1990.
For empty matrices that aren't 0-by-0, or for the 0-by-0 matrix where you specify a dimension input argument, we use the "first non-singleton dimension or specified dimension" rule for any other matrix.
sum(ones(0, 2)) % Result is 1-by-2 since first dimension is non-singular
ans = 1x2
0 0
sum(ones(2, 0)) % Result is 1-by-0 since first dimension is non-singular
ans = 1x0 empty double row vector
sum(ones(1, 0)) % Result is 1-by-1 since second dimension is non-singular
ans = 0
sum(ones(0, 0, 3), 2) % Result is 0-by-1-by-3 since dimension 2 was specified
ans = 0x1x3 empty double array
sum(ones(0, 0), 2) % Result is 0-by-1 since dimension 2 was specified
ans = 0x1 empty double column vector
Paul
Paul 2024 年 3 月 28 日
Hi Steven,
Thanks for your (as usual) thoughtful response.
As for the statement in the doc for any:
"B = any(A) tests along the first array dimension of A whose size does not equal 1,"
I hope you agree that a reasonable reader could interpret that as an unambiguous statement of fact for any allowable A. If the author intends that to be a statement of a general principle for which there may be one or more exceptions, then adding one word would convey that sentiment:
"B = any(A) usually/typically/generally tests along the first array dimension of A whose size does not equal 1,"
That one extra word would signal the reader that there may be exceptions to the stated rule. If the scalar is the only exception (is it?), then just say so:
"B = any(A) tests along the first array dimension of A whose size does not equal 1 when A is not a scalar,"
Of course, that would lead the curious reader to wonder about the case when A is a scalar, which may not be a bad thing.
If it were up to me, I'd not have a general statement about the "test" in the description and instead leave the heavy lifting to describe how any() applies the test for each individual case.
I acknowledged in my comment that the scalar case drops down to the vector case, but I still think it would be more logical to have four separate cases described: scalar, vector, matrix, multidimenstional array, which would work in parrallel with the four types of allowable arrays listed for the Input Array in the Input Arguments. The latter three of those cases would each describe the non-empty and empty variations.
Regarding the comments on sum ....
I should have been more clear that this section of the doc page explicitly calls out the 0 x 0 case, and so it seems natural to me that it would immediately be followed by a description of the treatment of the 0 x N, N x 0 and multidimensional empty cases.
I trust that "For empty matrices that aren't 0-by-0, ... , we use the "first non-singleton dimension ... rule" [as] for any other matrix. (emphasis added)" is an accurate description of the actual implementation.
But, this section of the doc page states: "If you do not specify the dimension, then the default is the first array dimension of size greater than 1."

サインインしてコメントする。

 採用された回答

Steven Lord
Steven Lord 2024 年 3 月 22 日

0 投票

Not a bug. On that documentation page, the description of the A input argument states in part "The any function ignores elements of A that are NaN (Not a Number)."

5 件のコメント

Bruno Luong
Bruno Luong 2024 年 3 月 22 日
移動済み: Bruno Luong 2024 年 3 月 22 日
OK I miss that. Thanks
James Tursa
James Tursa 2024 年 3 月 23 日
編集済み: James Tursa 2024 年 3 月 23 日
I seem to recall an earlier related post on this but I can't find it at the moment. I think it had to do with any() and all() treating NaN differently in different circumstances (scalar vs vector inputs?). I'm still looking for this post ...
Regardless, note that the NaN sentence is curiously absent in the all() doc.
*** Follow Up ***
It was Technical Support Case #06269659 and had to do with consistency of any() vs all() vs nnz() with regards to full and sparse variables.
Bruno Luong
Bruno Luong 2024 年 3 月 23 日
"Regardless, note that the NaN sentence is curiously absent in the all() doc."
Migh be because the end result is always the same with or without the presence of NaNs for ALL?
Only ANY needs this specific sentense to clear out the interpretation in the edhe cases.
I'm OK with mathwork design choice, however it is not very consistent across the board: mean, std functions do not ignore NaN by default, and any and all do, and there is no switch to control it.
James Tursa
James Tursa 2024 年 3 月 23 日
... and nnz() does not ignore NaN. E.g., you might expect these to give the same result but they don't:
any(nan)
ans = logical
0
nnz(nan)>0
ans = logical
1
Bruno Luong
Bruno Luong 2024 年 3 月 24 日
To defend TMW, there is no possible to convert NaN to logical, both commands throw an error (and rightly so)
logical(NaN)
NaN & true
So they select to ignore NaN when they appear in input argument of ANY and ALL.
NNZ is not intended for "logical" based.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

製品

リリース

R2023b

タグ

質問済み:

2024 年 3 月 22 日

コメント済み:

2024 年 3 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by