MATLAB Language Using functions with logical output All and Any with empty arrays

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

Special care needs to be taken when there is a possibility that an array become an empty array when it comes to logical operators. It is often expected that if all(A) is true then any(A) must be true and if any(A) is false, all(A) must also be false. That is not the case in MATLAB with empty arrays.

>> any([])
ans =
     0
>> all([])
ans =
     1

So if for example you are comparing all elements of an array with a certain threshold, you need to be aware of the case where the array is empty:

>> A=1:10;
>> all(A>5)
ans =
     0
>> A=1:0;
>> all(A>5)
ans =
     1

Use the built-in function isempty to check for empty arrays:

a = [];
isempty(a)
ans =
1


Got any MATLAB Language Question?