How to create a vector with for loops and if statements

Creating a vector based on the following;
t = linspace(-1,2,3001);
  • v = 1000*t when 0 <= t <= 1
  • v = 0 otherwise
I've tried multiple variations of the following, never getting the desired resultant vector.
for index = t
if t>=0 & t<=1
v = 1000*t;
else
v = 0;
end
end
This results in a 1x1 vector with 0
Next, added v = t*0 instead of just 0
t = linspace(-1,2,3001);
for index = t
if t>=0 & t<=1
v = 1000*t;
else
v = t*0;
end
end
This gives me the appropriately sized vector, but it's just full of 0's
So I attempted to simplify further;
t = linspace(-1,2,3001);
for index = t
if t < 0
v = t*0;
elseif t>1
v = t*0;
else
v = 1000*t;
end
end
This gives me a linear vector v=1000*t for all values, as if my IF statements are being ignored.
I've come to the conclusion that I'm going about this in some fundamentally incorrect manner and would appreciate some guidance.
edit: Still working on this... I see that no matter what I've attempted, the result is always the last statement (regardless of conditions). Clearly doing something wrong here.
Thanks in advance.

 Accepted Answer

Cedric
Cedric on 14 Apr 2013
Edited: Cedric on 14 Apr 2013
What about:
v = zeros(size(t)) ;
id = t >= 0 & t <= 1 ;
v(id) = 1000 * t(id) ;
or simpler and more efficient from the indexing point of view I guess:
v = 1000 * t ;
id = t < 0 | t > 1 ;
v(id) = 0 ;
PS: if you don't understand what I did here, look at the example that I gave there.

5 Comments

Phil
Phil on 14 Apr 2013
Edited: Phil on 14 Apr 2013
While that does indeed work (thank you), this is for a school assignment and specifically requires for loops & if statements. I continue to try multiple variations and never obtain the correct output.
I see that no matter what I try, the result is always the last statement (regardless of conditions). Clearly doing something wrong here.
Ok, you make the same mistake in all your loop variants actually. This
for index = t
will define index as t(1) in the first iteration, t(2) in the second, etc. This is not an index but a time, and you want an index for indexing relevant elements of t and v in the loop.
What you should do first is to define
t = [-1, -0.5, 0, 0.5, 1, 1.5, 2] ;
and display the steps of your computation (remove the ";" or display relevant variables), so you have an idea of what happens (and you can't do that when t has 3001 elements). Once you understand how it works and why it cannot be correct, you can think about a solution based on indexing.
In these situations, we usually build an index that goes from 1 to the number of elements of the vectors that we want to index (look at LENGTH or NUMEL). And in the loop, we index relevant vectors to work with their components, e.g.
a = 5 : 12 ;
b = zeros(1, 8) ;
for k = 1 : 8
if a(k) == 7
b(k) = -1 ;
end
end
Thank you. Will spend some time reading the recommended help files.
You're welcome. The trick is to work on an example involving vectors with a small amount of elements (not 3001), so you can visualize what happens. Once it is working, you scale up to 3001 elements.
PS: I edited a little the example, so you can copy paste and run it. If you run it, display variables a and b after the loop. You can also display them (as well as k) within the loop.
Again, thank you for the point in the right direction (and not just the answer). Did what you suggested, read the relevant help files and have now managed to get the desired output.

Sign in to comment.

More Answers (0)

Products

Asked:

on 14 Apr 2013

Community Treasure Hunt

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

Start Hunting!