What is the difference between "any" and "all" function?
显示 更早的评论
I am trying to learn the difference between any and all function but it seems like they both are equal and use for finding any non zero value. If anyone could explain with the difference between these two function, I will apprecite it a lot.
11 个评论
Gary Roth
2023-6-6
Here's something interesting.
I have a problem where a have a shipload of really long sparse arrays and I need to figure out which, in any, are all 0's.
If x is one such array, then
~any(x)
and
all(~x)
will give the same logical value, but the any version ran 16X faster than the all version.
James Tursa
2023-6-6
编辑:James Tursa
2023-6-6
I'm surprised at this result. Not that it is significantly faster, but that it is only 16X faster! I might have expected 100X faster or more. Using any(x) doesn't require temporary data copies, and if x is sparse there aren't many physical elements to actually examine. In fact, you don't even need to examine any elements ... you can simply look at the internal non-zero row and column indexing to get an answer directly. Using all(~x) first requires that the temporary ~x be calculated, which will of course contain mostly 1's if x is sparse so if the dimension is large then a huge temporary variable is created. The all(~x) call could also simply examine the internal non-zero row and column indexing to get a result, but that large temporary copy is a time and resourse killer.
What are the dimensions and sparsity of your variables? Are they double or logical?
If your sparse matrix is actually sparsely populated, any could return true as soon as it finds the first non-zero element in x. In your second call it's not the all call that's the problem. It's the negation.
~0
All those implicit 0 values in x that weren't actually stored? They become 1 values in ~x and those values do need to be stored. That takes additional memory, sometimes even more than storing the matrix as a full matrix!
allZero = sparse(5, 5);
noZero = sparse(double(~allZero));
fullNoZero = full(noZero);
whos allZero noZero fullNoZero
How many non-zero values do each of those sparse matrices have?
[nnz(noZero) nnz(allZero)]
Curiously, there is a discrepancy in these two functions for what "nonzero" means for NaN values. E.g.,
all(nan)
any(nan)
any(nan~=0)
The doc for any( ) states it will return true "if any element is a nonzero number or logical 1"
The doc for all( ) states it will return true "if the elements are all nonzero or logical 1"
This is a rather fine distinction between an element being a nonzero number and an element being nonzero, and one that I would not have expected. I guess any( ) doesn't consider NaN to be a nonzero number but all( ) considers NaN to be nonzero. I would have expected the same result for both functions in this case since NaN is not equal to 0, but I would be wrong.
The implications of this for sparse variables is that all( ) can very quickly look at the internal indexing to get a result, but any( ) can't do that since it has to examine the elements themselves to determine if they are NaN or not, so my comment above about any( ) quickly using the indexing is not true. Not sure what the benefit of this different distinction is.
James Tursa
2023-6-7
编辑:James Tursa
2023-6-7
I wrote a couple of mex functions to check this out for a large sparse vector filled with NaN values. Indeed, it looks like all( ) does the quick smart thing, whereas any( ) has to check all the values.
>> timeit(@()any(x))
ans =
0.0614 <----- Relatively large compared to the mex routine
>> timeit(@()anyx(x)) % mex function that simply looks at the internal indexing
ans =
7.0915e-07
>> timeit(@()all(x))
ans =
6.8104e-07
>> timeit(@()allx(x)) % mex function that simply looks at the internal indexing
ans =
7.6779e-07
Well, I thought I had it figured out. But then I did this test:
x = sparse(nan(100000000,1));
any(x)
any(x,1)
any(x,'all')
Why the different result for the 'all' option?
The answer ... any(x,'all') does the smart shortcut thing, ignoring its definition of nonzero number for the other forms:
x = sparse(nan(100000000,1));
timeit(@()any(x))
timeit(@()any(x,1))
timeit(@()any(x,'all'))
It was bothersome to me that any(nan) and all(nan) didn't do the same thing. But now any( ) isn't even self consistent. Maybe a bug report is in order.
Walter Roberson
2023-6-7
nnz(SparseArray) should be quite fast
x = sparse(nan(100000000,1));
nnz(x)
timeit(@()nnz(x))
@Walter Roberson It is. So nnz( ) definition of nonzero matches all( ) definition, so it must look at the internal indexing to get a quick result since the number of nonzeros is just sitting at the end of the Jc array. The any( ) function is out-of-bed with this for some reason I still can't understand.
x = nan(100000000,1); % full x
nnz(x)
timeit(@()nnz(x))
Note that in the full x case, there is no shortcut and you need to take the time to examine each element. In the sparse x case, the number of nonzero elements has already been calculated by MATLAB when building the sparse martix and is sitting at the end of the internal column indexing array Jc, so all you have to do is grab that element and return it. Very fast. But it relies on the definition of nonzero including NaN.
James Tursa
2023-6-12
Response from TMW:
There were 2 main concerns you had:
- Consistent treatment of NaN values for the “nnz”, “all”, and “any” functions
- Consistent treatment of NaN values between the sparse and regular matrices when using the “any” function
The second issue will be fixed in a future release to ensure consistency in the “any” function.
We will consider addressing the first issue but will first wait to see if other users have the same concern.
回答(5 个)
B = [0 0 0];
[any(B), all(B)]
C = [1 1 1];
[any(C), all(C)]
A = [0 1 0];
[any(A), all(A)]
If any elements are true, any is true.
all is only true if all elements are true. If any are false, all returns false.
any returns true if any of the elements are non-zero, while all returns true only if all of them are non-zero. For all to return true, there must not be a single 0 in the array. If there is even a single zero, then all() will return false.
v = [1 0 3 5]
any(v)
all(v) % Not all are non-zero because the second element is not non-zero
v = [1,2,3,4]
any(v)
all(v) % Every single element is non-zero -- ALL of them.
vec = [0 5 7];
Is any element of "vec" equal to zero ? Yes, the first one:
any(vec)
Are all elements of "vec" equal to zero ? No, only the first one:
all(vec)
There is one significant difference between any() and all(). Here are definitions:
(1) any() - any True if any element of a vector is a nonzero number or is
logical 1 (TRUE). any ignores entries that are NaN (Not a Number).
(2) all() - all True if all elements of a vector are nonzero.
A = [1 1 0 1 0];
any(A)
all(A)
Another example - B = [1 1 1 1 1];
B = [1 1 1 1 1];
any(B)
all(B)
Another example - C = [ 0 0 0 0 0];
C = [ 0 0 0 0 0];
any(C)
all(C)
Walter Roberson
2023-2-21
1 个投票
any: at least one of the inputs is non-zero
all: every input is non-zero
Mathematically, all(x) works out the same as ~any(~x)
- 0 0 any=false all=false
- 0 1 any=true all=false
- 1 0 any=true all=false
- 1 1 any=true all=true
any(x) is sum(x(:)~=0)>0
all(x) is sum(x(:)~=0)==numel(x)
类别
在 帮助中心 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!