1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Very simple matlab question

  1. Oct 31, 2009 #1
    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

    this gives:

    why is it giving me 11 elements? with two 10's?
    i just want:
  2. jcsd
  3. Oct 31, 2009 #2


    User Avatar
    Science Advisor
    Gold Member

    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

    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.
  4. Oct 31, 2009 #3
    thanks, that works for me.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook