How to find out if a logical array is all zeros?
Show older comments
I have a logical array that is 100x1 logical. sometimes it contains a mix of numbers and sometimes it contains all 0s.
How would i go about making an if statement that checks to see if all 100x1 is 0s and if all 0s = do something
I thought i was correct in using any command but im not sure that works correctly for me..
Thanks!
1 Comment
Walter Roberson
on 14 Jan 2025
rise() is an undefined function.
Accepted Answer
More Answers (4)
Turlough Hughes
on 3 Feb 2022
One way:
~any(x)
William Rose
on 3 Feb 2022
2 votes
@MKM,
If A is a vector,
sum(abs(A))
will be zero, or not, of all the elements are zero, or not.
If A is a 2D array, do
sum(sum(abs(A)))
David Hill
on 3 Feb 2022
if all(~yourArray)
For a vector, all with one input will work.
x = ones(10, 1);
all(x > 0) % true
For a non-vector array you probably want to specify a dimension. In recent releases, you can specify a vector of dimensions on which to operate or you can specify 'all' as the dimension to operate on all the dimensions no matter how many there are.
x = ones(10, 10);
all(x > 0) % Only operate on dimension 1
all(x > 0, 'all') % Operate on dimensions 1 and 2
x = ones(10, 10, 10);
all(x > 0, [1 3]) % Operate on dimensions 1 and 3
all(x > 0, 'all') % Operate on dimensions 1, 2, and 3
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!