Why does any ('') return a logical 0 and all ('') return a logical 1?

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

If the function anydoes not consider the empty string ('') to be non-zero, then the function allshould do the same, but from the result the function allseems to consider the empty string ('') to be non-zero.

By the way, a similar thing happens where it any(NaN)returns a logical 0, and all(NaN)returns a logical 1.

Is this a MATLAB error?

Here is the information about the MATLAB version I'm using.
MATLAB Version: 9.1.0.441655 (R2016b)
MATLAB License Number: DEMO

+4
source share
4 answers

According to the documentation, the
definition of any :

any (x) ... determines if any element is a nonzero number or a logical 1 (true)

In practice, it anyis a natural extension of the logical operator OR.

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

and definition of all :

all (x) ... determines whether all elements are nonzero or logical 1 (true)

In practice, it allis a natural extension of the logical AND operator.

A - 0--0, all(A) 1 (true).

:

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

:

-In any , [ ], , , false.
- any OR, ||
- nonzero , V(k) ~= 0
- numbers, NaN - Not a Number, ~isnan(V(k)).

-In all , [ ], , , true
- all AND, &&
- V(k) ~= 0
- all , ~isnan(V(k))

+5

0, , . any , .

1, . , , .

+2

w.r.t .

  • 0
  • 1
  • - false

: , .

+1

any all .

Function documentation any
https://www.mathworks.com/help/matlab/ref/any.html

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

Function documentation all
https://www.mathworks.com/help/matlab/ref/all.html

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

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

0
source

Source: https://habr.com/ru/post/1671668/


All Articles