MATLAB Answers

Comparing a value against a set of values

4 views (last 30 days)
t4741
t4741 on 10 Sep 2019
Answered: Bruno Luong on 10 Sep 2019
I have a 1x1xn array of numbers that range from 0-255 (RGB), where n can be any number. Let's call it data.
I want to check if all the values of data are zero. If any of them are non-zero i want it to run a given command, and if they are all zero then I want it to not run the command
How can i do this?

  0 Comments

Sign in to comment.

Answers (2)

Bruno Luong
Bruno Luong on 10 Sep 2019
if any(A,3)
% ...
end

  0 Comments

Sign in to comment.


Raj
Raj on 10 Sep 2019
Edited: Raj on 10 Sep 2019
A=rand(1,1,n) % Define your matrix here
B=nonzeros(A);
if isempty(B)
%%Do nothing
else
%%Run command here
end

  2 Comments

Andrei Bobrov
Andrei Bobrov on 10 Sep 2019
A=rand(1,1,n) % Define your matrix here
if all(A)
%%Run command here
end
Raj
Raj on 10 Sep 2019
Obviously this is a better solution. Thanks!

Sign in to comment.

Sign in to answer this question.

Tags