how do I apply an if statement to each row of a column vector?

4 vues (au cours des 30 derniers jours)
Ben Newton
Ben Newton le 15 Oct 2020
Commenté : Sudhakar Shinde le 16 Oct 2020
i have a 1x10 column vector consisting of random numbers.
and i want to apply the following if statement to each number in the column vector.
if R < 0.5;
step = -1;
elseif R > 0.5;
step = 1;
end
where R is the column vector, and so the solution would be a 1x10 column vector consisting of either +1 or -1
any help is appreciatied, thanks.

Réponse acceptée

Sudhakar Shinde
Sudhakar Shinde le 15 Oct 2020
Modifié(e) : Sudhakar Shinde le 15 Oct 2020
Try this:
R = [0.1:0.1:1];
step=zeros(1,length(R(:)));
for n=1:length(R(:))
if R(n)<0.5
step(n)= -1;
elseif R(n) > 0.5
step(n) = 1;
end
end
  2 commentaires
Ben Newton
Ben Newton le 15 Oct 2020
thanks for your help
Sudhakar Shinde
Sudhakar Shinde le 16 Oct 2020
Welcome. Glad to help.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur NaNs dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by