# ANY and ALL with NaN array

13 views (last 30 days)
Bruno Luong on 22 Mar 2024
Commented: Paul on 28 Mar 2024
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?
Steven Lord on 26 Mar 2024
@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 on 28 Mar 2024
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 on 22 Mar 2024
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)."
James Tursa on 23 Mar 2024
... 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 on 24 Mar 2024
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.

### Categories

Find more on Numeric Types in Help Center and File Exchange

R2023b

### Community Treasure Hunt

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

Start Hunting!