Question about for loop
Mostrar comentarios más antiguos
I'm trying to solve this question.
A vector is given by x=[-3.5 -5 6.2 11 0 8.1 -9 0 3 -1 3 2.5]. Using conditional statement and loops, write a program that crates two vectors from x- one(P) that contains all positive elements of x, and a second(N) that contains the negative elements of x. The elements in new vectors are in the same order as in x.
I totally understand the conditional statement part, but for the loop part, I only worked this fragment out then don't know how to continue:
x=[-3.5 -5 6.2 11 0 8.1 -9 0 3 -1 3 2.5];
P=[];
N=[];
a=length(x);
for k=1:a
if x>0
Someone please help! Thanks!
1 comentario
Irfan Muhammad
el 21 de Dic. de 2019
x=[-3.5 -5 6.2 11 0 8.1 -9 0 3 -1 3 2.5];
P=[];
N=[];
for k=1:length(x)
if x(k)>=0
P(k)=x(k);
else if x(k)<0
N(k+2)=x(k)
end
end
end
N=circshift(N,-2)
Respuestas (1)
Walter Roberson
el 6 de Nov. de 2013
P(end+1) = x
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!