4

I just found the statement any('') returns a logical 0, while the statement all('') returns a logical 1.

If the function any does not think the empty string ('') as nonzero, the function all should do the same, but from the result, the function all seems to think the empty string ('') as nonzero.

BTW, the similar thing happens where any(NaN) returns logical 0 while all(NaN) returns logical 1.

Is it a MATLAB bug?

Here is the version information of the MATLAB I am using.
MATLAB Version: 9.1.0.441655 (R2016b)
MATLAB License Number: DEMO

Ray Chen
  • 182
  • 1
  • 2
  • 13
  • Maybe add it to the list http://stackoverflow.com/questions/1710299/corner-cases-unexpected-and-unusual-matlab?rq=1 – Thilo Mar 07 '17 at 04:15
  • 9
    It’s not a bug. any(X) means you can find an element of X that is true; for no elements, you can’t. all(X) means you can’t find an element of X that is false; for no elements, you can’t. Compare ∀, ∃, and see https://stackoverflow.com/questions/2195289/why-does-iqueryable-all-return-true-on-an-empty-collection, https://stackoverflow.com/questions/30223079/why-does-stream-allmatch-return-true-for-an-empty-stream, https://stackoverflow.com/questions/19601802/how-does-all-in-python-work-on-empty-lists, https://stackoverflow.com/questions/16662727/why-does-all-return-true-on-an-empty-array. – Ry- Mar 07 '17 at 04:16

4 Answers4

5

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))

Community
  • 1
  • 1
rahnema1
  • 15,264
  • 3
  • 15
  • 27
  • Thanks for so detailed reply. I think I understand your explanation. I also double checked the documentation (indeed, the `any` function ignores NaN values while the `all` function doesn't mention it) and run the 2 functions provided by you, and the results prove your explanation. – Ray Chen Mar 07 '17 at 14:54
2

Any returns 0 because it is not the case that any of its elements are true. An any clause is true if any of its elements are true.

All returns 1 because it is the case that all of its elements are true. All of its elements are true so long as none of its elements are false, and none of its elements are false.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
1

It is convenient and consistent w.r.t the rest of your math when applying an associative operation over an empty list of values returns the neutral element of that operation. This is why

  • a sum of zero numbers is 0
  • a product of zero numbers is 1
  • a logical or of zero booleans is false
  • a logical and of zero booleans is true

Keywords for further reading: monoid, fold.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
0

I just found solid proof from the documentation about the any and all functions, as follows.

Documentation of the function any
https://www.mathworks.com/help/matlab/ref/any.html

If A is an empty 0-by-0 matrix, any(A) returns logical 0 (false).

Documentation of the function all
https://www.mathworks.com/help/matlab/ref/all.html

If A is an empty 0-by-0 matrix, then all(A) returns logical 1 (true).

And the empty string in MATLAB is actually a 0×0 empty char array (I just found it). That explains my original question, from the documentation.

Ray Chen
  • 182
  • 1
  • 2
  • 13