ANY and ALL with NaN array
6 ビュー (過去 30 日間)
古いコメントを表示
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
any(A) % Not expected
What do I miss? Any comment? Should I call it a bug?
8 件のコメント
Steven Lord
2024 年 3 月 26 日
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)
d2 = any(A, 3)
@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
sum(ones(2, 0)) % Result is 1-by-0 since first dimension is non-singular
sum(ones(1, 0)) % Result is 1-by-1 since second dimension is non-singular
sum(ones(0, 0, 3), 2) % Result is 0-by-1-by-3 since dimension 2 was specified
sum(ones(0, 0), 2) % Result is 0-by-1 since dimension 2 was specified
Paul
2024 年 3 月 28 日
Hi Steven,
Thanks for your (as usual) thoughtful response.
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.
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
2024 年 3 月 22 日
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 件のコメント
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)
nnz(nan)>0
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Numeric Types についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!