ANY and ALL with NaN array
古いコメントを表示
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 件のコメント
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
2024 年 3 月 22 日
編集済み: Bruno Luong
2024 年 3 月 22 日
Bruno Luong
2024 年 3 月 24 日
編集済み: Bruno Luong
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)
or a matrix case
ismatrix(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)
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.
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."
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!