What is the difference between "any" and "all" function?
352 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
James Tursa
el 7 de Jun. de 2023
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
el 12 de Jun. de 2023
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.
Respuestas (5)
Image Analyst
el 21 de Feb. de 2023
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.
0 comentarios
Torsten
el 21 de Feb. de 2023
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)
0 comentarios
Sulaymon Eshkabilov
el 21 de Feb. de 2023
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)
0 comentarios
Walter Roberson
el 21 de Feb. de 2023
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)
0 comentarios
Ver también
Categorías
Más información sobre Performance and Memory en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!