From: Mark Hard on
Hi guys,I have some lines of code inside a for loop. Say for example I have,

for i = 1:10

y = x1_1 + x2_1;

end

What I really want is for when i = 1 to use the variable called x1_1, when i = 2 use x1_2, when i = 3 use x1_3 ect. (Similarly for x2_1)

Is it possible to call the variable x1_i or use a command that would allow me to do this?

As always, helpis greatly appreciated.

Mark.
From: dpb on
Mark Hard wrote:
> Hi guys,I have some lines of code inside a for loop. Say for example I
> have,
>
> for i = 1:10
>
> y = x1_1 + x2_1;
>
> end
>
> What I really want is for when i = 1 to use the variable called x1_1,
> when i = 2 use x1_2, when i = 3 use x1_3 ect. (Similarly for x2_1)
>
> Is it possible to call the variable x1_i or use a command that would
> allow me to do this?
....

Possible, yes...good idea? Probably not.

See FAQ

<http://matlabwiki.mathworks.com/MATLAB_FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F>

for the "why" and ways around...

--
From: Mark Hard on
dpb <none(a)non.net> wrote in message <hj271j$v0e$1(a)news.eternal-september.org>...
> Mark Hard wrote:
> > Hi guys,I have some lines of code inside a for loop. Say for example I
> > have,
> >
> > for i = 1:10
> >
> > y = x1_1 + x2_1;
> >
> > end
> >
> > What I really want is for when i = 1 to use the variable called x1_1,
> > when i = 2 use x1_2, when i = 3 use x1_3 ect. (Similarly for x2_1)
> >
> > Is it possible to call the variable x1_i or use a command that would
> > allow me to do this?
> ...
>
> Possible, yes...good idea? Probably not.
>
> See FAQ
>
> <http://matlabwiki.mathworks.com/MATLAB_FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F>
>
> for the "why" and ways around...
>
> --

Thanks I understand it now.