Evaluating Matlabs of everyone and everyone with an empty vector gives different answers

Everything and everything on an empty should give the same answer. But here they give a different answer, and all this is empty. Can anyone explain this behavior of matlabs (tested on version 2010a-> 2012b).

β†’ any ([])

ans =

0 

but

β†’ all ([])

ans =

  1 
+4
source share
2 answers

any and all in an empty array should absolutely not give the same answers - the behavior of MATLAB is documented and correct.

any(A) means "there is an element A that is true," in other words

  βˆƒx∈A x is true 

Since there are no elements in A , it correctly returns false .

all(A) means "for all elements in A , this element is true", in other words

  βˆ€x∈A x is true 

This may be less intuitive, but since there are no elements in A , this sentence is true - and MATLAB correctly returns true . Any first-order logic tutorial will confirm this.

If you need a case where the behavior of MATLAB really seems wrong and inconsistent above, try

 >> if [] disp('hello'); else disp('bye'); end bye 

In all other cases, if X is true when all elements from X are true. But when X [] , if behaves differently. It is also documented.

+2
source

This behavior is explicitly documented:

I believe that the behavior of all somewhat contrary to intuition. By assumption, I would say that the intention is this: for symmetry:

 ~all(A) == any(~A) 
+5
source

All Articles