How to separate a vector into positive and negative vectors using a for loop?

clc;
x = [-3.6 10 3 -1 11.4 0 7 -9.5 2 15 -1 3];
for i = 1:length(x)
if x(i) >= 0
x(i) = P
elseif x(i) < 0
x(i) = N
end
end
This is what I have. It is obviously wrong but you should get the gist of it. It needs to be separated into two vectors: N and P

Respuestas (2)

Andrei Bobrov
Andrei Bobrov el 31 de Mzo. de 2019
Editada: Andrei Bobrov el 31 de Mzo. de 2019
x = [-3.6 10 3 -1 11.4 0 7 -9.5 2 15 -1 3];
y = strings(numel(x),1);
for ii = 1:length(x)
if x(ii) >= 0
y(ii) = "P";
elseif x(ii) < 0
y(ii) = "N";
end
end
or
x = [-3.6 10 3 -1 11.4 0 7 -9.5 2 15 -1 3];
cn = 0;
cp = 0;
nn = numel(x);
p = zeros(nn,1);
n = zeros(nn,1);
for ii = 1:nn
if x(ii) >= 0
cp = cp + 1;
p(cp) = x(ii);
else
cn = cn + 1;
n(cn) = x(ii);
end
end
p = p(1:cp);
n = n(1:cn);
or
p = x(x >= 0);
n = x(x < 0);
You're not supposed to assign x - that's a given constant. You're supposed to build N and P. So it will be more like
clc;
x = [-3.6 10 3 -1 11.4 0 7 -9.5 2 15 -1 3];
nIndex = 1;
pIndex = 1;
for i = 1:length(x)
if x(i) >= 0
P(pIndex) = x(i);
pIndex = .......
elseif x(i) < 0
N(nIndex) = x(i);
nIndex = .............
end
end
See if you can complete your homework from the above.

8 comentarios

keeps saying illegal use of elseif
Tell me how you completed the lines I left for you to do. These lines:
pIndex = .......
and
nIndex = .............
What did you do to finish those lines? It's really trivial. What do you think you should do to those two variables to increment them to the next available index in the array?
Did you see where I said "See if you can complete your homework from the above." That means you have to do your homework yourself. I imagine your professor did not say "Ask this question on the internet and then post someone's working code as your own."
no need to get sassy bro. I played around with what i knew and nothing worked that's why i came here. I thought the "...." was part of the code, that was misunderstood. I will try again
I'm still not quite sure what to do here
Increment means to add one. Here's the solution for one of them
nIndex = nIndex + 1;
You should be able to complete the one and only line left for you to complete. It's pretty analogous to the line above.
That is one of the things I tried. It still does not work
Don't worry about it though. I just did it a completely different way and it worked
I tried it both before I posted, and after your last post, and it works perfectly fine.
The final line to get it to work is
pIndex = pIndex + 1;
Glad you found an alternate way that is 100% your own though - that's probably better as far as turning in graded homework goes.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Versión

R2018b

Etiquetas

Preguntada:

el 31 de Mzo. de 2019

Comentada:

el 1 de Abr. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by