PDA

View Full Version : very simple matlab question


sara_87
Oct31-09, 07:39 AM
1. The problem statement, all variables and given/known data

i want to produce a vector t(i) where t(i)=i and i=1:10

2. Relevant equations



3. The attempt at a solution

for i=1:10
t(i)=i
end

this gives:
1,2,3,4,5,6,7,8,9,10,10

why is it giving me 11 elements? with two 10's?
i just want:
1,2,3,4,5,6,7,8,9,10

jamesrc
Oct31-09, 01:31 PM
It's possible t was already defined so that even though that part of your script should work, it is not changing the value of t(11). Try clearing the value first:

clear t
for i = 1:10
t(i) = i
end

Alternatively, you can use vectorized code:

t = 1:10;

This is cleaner, takes advantage of what MATLAB can do, and wouldn't require you to clear the variable first.

sara_87
Oct31-09, 04:37 PM
thanks, that works for me.