Contradictions in ANY and ALL?

Can someone please explain why MATLAB produces
any([]) = 0,
but we get
all([]) = 1 ?
This seems like a contradiction. The answer ought to be 1 for both.

 Accepted Answer

Probably the best way to see the logic behind TMW's choice is to consider the two identities:
any(union(A,B)) = any(A) | any(B)
all(union(A,B)) = all(A) & all(B)
where A and B are logical vectors. These are obviously both true whenever A and B are both non-empty. However, for them to remain identically true if either is empty would necessitate the definitions that TMW gives to 'any' and 'all' for empty vectors. They are very natural extensions of the definitions of 'any' and 'all' to empty vectors.
Another way of envisioning this is to imagine removing elements of A one at a time in 'any(A)'. The only possible change in truth value would be from true to false, never from false to true. That means the definition for any([]) had better be false if this is always to hold when the last element is removed. Similarly if the elements of A are removed one at a time in 'all(A)', the only possible change would be from false to true, never from true to false, and the definition of all([]) should therefore naturally be true by the same reasoning.

More Answers (1)

James Tursa
James Tursa on 8 Jul 2015
Edited: James Tursa on 8 Jul 2015
Only TMW can explain their reasoning, but this behavior is specifically documented for both functions. What doesn't seem to be documented is the behavior for other sized empty matrices/arrays.

Categories

Find more on Functions in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!