I am trying to create two vectors out of one,x, where one will contain all the positive elements and the other all the negative elements.

%this program creates vectors N and P out of x
%N will contain negative elements and P the positive elements
x=[-3.5 -5 6.2 11 0 8.1 -9 0 3 -1 3 2.5];
for k=length(x);
if x>=0
k=x
P(k)=k
else x<0
N(k)=k
end
end
P
N

 Accepted Answer

Just use ‘logical indexing’:
x=[-3.5 -5 6.2 11 0 8.1 -9 0 3 -1 3 2.5];
P = x(x >= 0);
N = x(x < 0);

15 Comments

In that instance, change the first line of your for loop and index every value of ‘x’:
for k = 1:length(x);
if x(k)>=0
P(k)=x(k);
else x(k)<0
N(k)=x(k);
end
end
I would create separate counters for ‘P’ and ‘N’ (perhaps ‘kp’ and ‘kn’, initialising them to zero before the loop and incrementing them just before each respective ‘P’ and ‘N’ assignment) but your loop should work with the changes here.
This is now my result:
x=[-3.5 -5 6.2 11 0 8.1 -9 0 3 -1 3 2.5];
for k =length(x);
if x(k)>0
P(k)=x(k);
else x(k)<0
N(k)=x(k);
end
end
P
N
P =
Columns 1 through 7
6.2000 11.0000 8.1000 3.0000 3.0000 2.5000 0
Columns 8 through 12
0 0 0 0 2.5000
N =
Columns 1 through 7
-3.5000 -5.0000 3.0000 4.0000 0 0 -9.0000
Columns 8 through 12
0 0 -1.0000 0 12.0000
You ignored my comment about the else statement. Get rid of the x(k)<0 part. Just make it a simple "else" on that line with nothing else on the line.
I did it:
x=[-3.5 -5 6.2 11 0 8.1 -9 0 3 -1 3 2.5];
for k =length(x);
if x(k)>=0
P = x(x>0);
else
N =x(x<0)
end
end
P
N
P =
6.2000 11.0000 8.1000 3.0000 3.0000 2.5000
N =
Columns 1 through 7
-3.5000 -5.0000 3.0000 4.0000 0 0 -9.0000
Columns 8 through 12
0 0 -1.0000 0 12.0000
Great!
I would add one refinement that I mentioned earlier with respect to separate incremental counters for ‘P’ and ‘N’:
kp = 0;
kn = 0;
for k = 1:length(x);
if x(k)>=0
kp = kp + 1;
P(kp)=x(k);
else
kn = kn + 1;
N(kn)=x(k);
end
end
P
N
With this you get:
P =
6.2 11 0 8.1 0 3 3 2.5
N =
-3.5 -5 -9 -1
I still don't know what's up with vector N
x=[-3.5 -5 6.2 11 0 8.1 -9 0 3 -1 3 2.5];
kp = 0;
kn = 0;
for k = 1:length(x);
if x(k)>=0
kp = kp + 1;
P(kp)=x(k);
else
kn = kn + 1;
N(kn)=x(k);
end
end
P
N
P =
Columns 1 through 7
6.2000 11.0000 0 8.1000 0 3.0000 3.0000
Column 8
2.5000
N =
Columns 1 through 7
-3.5000 -5.0000 -9.0000 -1.0000 0 0 -9.0000
Columns 8 through 12
0 0 -1.0000 0 12.0000
When I run it I get this:
P =
6.2000 11.0000 0 8.1000 0 3.0000 3.0000 2.5000
N =
-3.5000 -5.0000 -9.0000 -1.0000
So I suspect you have previous P and N vectors hanging around from a previous run. Clear them first before running your code:
clear P N
You copied and pasted my code exactly (as far as I can tell), so the only reason you could be coming up with a different result is if ‘N’ already exists in your workspace.
Before the beginning of your loop, just before the ‘kp’ assignment, insert this line:
clear N P
and see if that changes your result.
Thanks a lot. This is difficult for me but I'm learning. Would it be easier to do this problem using while loop?
No. A while loop is used typically when you don't necessarily know the number of iterations to be performed ahead of time ... there is just some condition that needs to be met for the loop to terminate. In your case, you know the number of iterations you need precisely (the length of x), so a for loop is appropriate.
My pleasure.
We all had to start knowing nothing about programming, so we will help here as much as we can.
A for loop with a contained if block is best for this problem. A while loop would add the complexity of another counter (for ‘k’), and not add any benefit. If, for instance, you had to use a loop to test for the first negative value in ‘x’ and then stop, a while loop would be the better choice. You have to iterate through all of ‘x’ regardless, so a for loop (that takes care of the loop increments efficiently) is preferable here.

Sign in to comment.

More Answers (1)

You are using k as a loop variable but only doing one iteration, k = 12. Maybe you want this result:
P = x(x>0);
N = x(x<0);
Or if you want them to retain their original spots, something like this
P = x;
P(~(x>0)) = 0;
N = x;
N(~(x<0)) = 0;
EDIT:
If you have to use loops, then change your looping index to the following:
for k=1:length(x)
And inside your loop, don't use x by itself in the test since that is the entire vector x. Instead, use the k'th element of x (i.e., use your k index). E.g.,
if x(k) >= 0
Finally, Don't change the value of k inside your loop! That will mess up the looping. So get rid of this line:
k=x
And then use x(k) on the right hand side of your assignments instead of k.

3 Comments

It seemed to solve the problem, P is now a vector but vector N is not coming out.
%this program creates vectors N and P out of x
%N will contain negative elements and P the positive elements
x=[-3.5 -5 6.2 11 0 8.1 -9 0 3 -1 3 2.5];
for k=length(x);
if x(k)>=0
P = x(x>0);
else x(k)<0
N=x(x<=0)
end
end
P
N
P =
6.2000 11.0000 8.1000 3.0000 3.0000 2.5000
N =
1 2 3 4 0 0 0 0 0 0 0 12
LOL. Well, if you just change your else statement to this (i.e., drop the x(k) < 0 part) you will have something:
else
However, your "loop" is basically solving the entire problem in a vectorized manner at each iteration! So, this is not really in the spirit of the looping requirement.
I would take a look at Star Strider's comment since he has it basically laid out for you in detail (although it looks like he needs to fix the else line as well).

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!