According to the Documentation
definition of any:
any(x) ...determines if any element is a nonzero number or logical 1 (true)
In practice, any
is a natural extension of the logical OR operator.
If A is an empty 0-by-0 matrix, any(A)
returns logical 0 (false).
and definition of all:
all(x) ...determines if the elements are all nonzero or logical 1 (true)
In practice, all
is a natural extension of the logical AND operator.
If A is an empty 0-by-0 matrix, then all(A)
returns logical 1 (true).
We can implement both functions:
function out = Any(V)
out = false;
for k = 1:numel(V)
out = out || (~isnan(V(k)) && V(k) ~= 0);
end
end
function out = All(V)
out = true;
for k = 1:numel(V)
out = out && (V(k) ~= 0);
end
end
Explanation:
-In any
we assume that all elements are not nonzero [so all are zeros] and we want to prove that the assumption is wrong so we provide an initial value of false
.
-Because any
is a natural extension of the logical OR operator we use ||
-Because we should check for nonzero
numbers we use V(k) ~= 0
-Because we should check for nonzeros numbers
and NaN
is Not a Number
we use ~isnan(V(k))
.
-In all
we assume that all elements are nonzero [so all are ones] and we want to prove that the assumption is wrong so we provide an initial value of true
-Because all
is a natural extension of the logical AND operator we use &&
-Because we should check for nonzeros we use V(k) ~= 0
-Because the definition of all
doesn't force that nonzero elements to be numbers we don't use ~isnan(V(k))