1. Not finding help here? Sign up for a free 30min 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!

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
    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
     
  2. jcsd
  3. Oct 31, 2009 #2

    jamesrc

    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
    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.
     
  4. Oct 31, 2009 #3
    thanks, that works for me.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Very simple matlab question
  1. Very simple RC-circuit (Replies: 11)

Loading...