How to find out if a logical array is all zeros?

조회 수: 125 (최근 30일)
MKM
MKM 2022년 2월 3일
댓글: Walter Roberson 2025년 1월 15일
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!

채택된 답변

John D'Errico
John D'Errico 2022년 2월 3일
편집: John D'Errico 2022년 2월 3일
any does work, with a slight tweak. But you could also use all, or sum, or nnz, or even find. There are certainly many ways.
if ~any(X)
...
end
if all(~X)
...
end
if sum(x) == 0
...
end
if numel(find(x)) == 0
...
end
if nnz(x) == 0
...
end
Any of those tools would suffice.
  댓글 수: 3
James Tursa
James Tursa 2025년 1월 15일
Note that any( ) is possibly written to short-circuit in the background, as these timings suggest:
x = false(1,100000000);
y = x;
x(end) = true; % last element is true
y(1) = true; % first element is true
timeit(@()~any(x)) % has to examine all of the elements to find the true one?
ans = 0.0230
timeit(@()~any(y)) % once first element is examined the answer is known
Warning: The measured time for F may be inaccurate because it is running too fast. Try measuring something that takes longer.
ans = 0
Walter Roberson
Walter Roberson 2025년 1월 15일
No, it is just very fast at checking from the beginning of the array.
Some of my tests on y show times on the order of 1e-9 instead of 0, by the way.
x = false(1,100000000);
y = x;
x(end) = true; % last element is true
y(1) = true; % first element is true
timeit(@()~any(x)) % has to examine all of the elements to find the true one?
ans = 0.0234
timeit(@()~any(y)) % once first element is examined the answer is known
Warning: The measured time for F may be inaccurate because it is running too fast. Try measuring something that takes longer.
ans = 0
timeit(@()~any(y)) % once first element is examined the answer is known
Warning: The measured time for F may be inaccurate because it is running too fast. Try measuring something that takes longer.
ans = 0
y(5000) = true;
timeit(@()~any(y)) % has to examine all of the elements to find the true one?
Warning: The measured time for F may be inaccurate because it is running too fast. Try measuring something that takes longer.
ans = 0
timeit(@()~any(y)) % once first element is examined the answer is known
Warning: The measured time for F may be inaccurate because it is running too fast. Try measuring something that takes longer.
ans = 0
timeit(@()~any(y)) % once first element is examined the answer is known
Warning: The measured time for F may be inaccurate because it is running too fast. Try measuring something that takes longer.
ans = 0
y(1) = true;
timeit(@()~any(y))
Warning: The measured time for F may be inaccurate because it is running too fast. Try measuring something that takes longer.
ans = 0
timeit(@()~any(y))
Warning: The measured time for F may be inaccurate because it is running too fast. Try measuring something that takes longer.
ans = 0
timeit(@()~any(y))
Warning: The measured time for F may be inaccurate because it is running too fast. Try measuring something that takes longer.
ans = 0
y(1) = false;
timeit(@()~any(y)) % once first element is examined the answer is known
ans = 1.0476e-06
timeit(@()~any(y)) % once first element is examined the answer is known
ans = 1.0646e-06
timeit(@()~any(y)) % once first element is examined the answer is known
ans = 1.0516e-06

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

Turlough Hughes
Turlough Hughes 2022년 2월 3일
One way:
~any(x)

William Rose
William Rose 2022년 2월 3일
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
David Hill 2022년 2월 3일
if all(~yourArray)

Steven Lord
Steven Lord 2022년 2월 3일
For a vector, all with one input will work.
x = ones(10, 1);
all(x > 0) % true
ans = logical
1
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
ans = 1×10 logical array
1 1 1 1 1 1 1 1 1 1
all(x > 0, 'all') % Operate on dimensions 1 and 2
ans = logical
1
x = ones(10, 10, 10);
all(x > 0, [1 3]) % Operate on dimensions 1 and 3
ans = 1×10 logical array
1 1 1 1 1 1 1 1 1 1
all(x > 0, 'all') % Operate on dimensions 1, 2, and 3
ans = logical
1
  댓글 수: 1
MKM
MKM 2022년 2월 4일
This is great! Thanks for taking the time to help me with this.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Spline Postprocessing에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by