Results 1 to 2 of 2

Math Help - Matlab-change of variable name inside a loop

  1. #1
    Member
    Joined
    Feb 2010
    Posts
    87

    Matlab-change of variable name inside a loop

    for i=1: 5
    x(i)=2*i^2;
    end

    the above program yields x as a matrix.
    But I want the variable name to change for every i. i.e., the 2*1^2 should be assigned to x1, 2*2^2 should be assigned to x2 e.t.c . In other words, inside the loop i should concatenates with x to form a new variable where the value of 2*i^2 will be stored.

    How to do it? Please help
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Member
    Joined
    Apr 2012
    From
    Erewhon
    Posts
    195
    Thanks
    127

    Re: Matlab-change of variable name inside a loop

    Code:
      for idx=1:5
        vv=['x' , num2str(idx)];
        assignin('base' , vv , 2*idx^2);
      end
    or:
    Code:
      for idx=1:5
         str=['x',num2str(idx),'=2*idx^2'];
         eval(str);
      end
    .
    Last edited by zzephod; October 1st 2013 at 02:54 AM.
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Replies: 3
    Last Post: March 25th 2013, 12:06 AM
  2. Replies: 2
    Last Post: February 27th 2013, 10:22 PM
  3. area inside the inner loop limacon
    Posted in the Calculus Forum
    Replies: 1
    Last Post: July 21st 2010, 11:56 AM
  4. [SOLVED] Finding the area inside a loop?
    Posted in the Calculus Forum
    Replies: 2
    Last Post: November 20th 2008, 03:08 AM
  5. change of variable
    Posted in the Calculus Forum
    Replies: 1
    Last Post: October 25th 2008, 01:26 AM

Search Tags


/mathhelpforum @mathhelpforum