From: Kashif on 19 Jun 2010 21:39 State_1=10; State_2=20; State_3=30; for M=1:3 State_x=State_{M} end How to use M such that State_x iteratively gets the above values.
From: ImageAnalyst on 19 Jun 2010 21:45 Kashif : See the FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F The recommended way is this: State_1=State_{1} ; State_2=State_{2}; State_3=State_{3}; especially when you only have a few variables. Otherwise make an array for M=1:3 NewState(M) = State_{M}; end
From: John D'Errico on 20 Jun 2010 08:12 "Kashif " <rajakashif(a)gmail.com> wrote in message <hvjrfr$hp3$1(a)fred.mathworks.com>... > State_1=10; > State_2=20; > State_3=30; > > for M=1:3 > State_x=State_{M} > end > > How to use M such that State_x iteratively gets the above values. The answer is DON'T DO IT! Use a cell array instead, or any array that is appropriate. Preallocate your arrays when you do this sort of thing if possible. Generating long lists of numbered arrays in a loop is a poor way to write code in general. John
From: us on 20 Jun 2010 09:13 "Kashif " <rajakashif(a)gmail.com> wrote in message <hvjrfr$hp3$1(a)fred.mathworks.com>... > State_1=10; > State_2=20; > State_3=30; > > for M=1:3 > State_x=State_{M} > end > > How to use M such that State_x iteratively gets the above values. a hint: http://matlabwiki.mathworks.com/MATLAB_FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F us
From: Kashif on 23 Jun 2010 11:28 "John D'Errico" <woodchips(a)rochester.rr.com> wrote in message <hvl0j9$fll$1(a)fred.mathworks.com>... > "Kashif " <rajakashif(a)gmail.com> wrote in message <hvjrfr$hp3$1(a)fred.mathworks.com>... > > State_1=10; > > State_2=20; > > State_3=30; > > > > for M=1:3 > > State_x=State_{M} > > end > > > > How to use M such that State_x iteratively gets the above values. > > The answer is DON'T DO IT! > > Use a cell array instead, or any array that is > appropriate. Preallocate your arrays when you > do this sort of thing if possible. > > Generating long lists of numbered arrays in a > loop is a poor way to write code in general. > > John Thanks all for the help. I guess I dint convey my point properly. My question was how to manipulate the variable name on each iteration. I guess some string operation are involved. Need help on that. I do understand that I can do it as below plus I can also use arrays to assign values if there are large no of states. State_1 = [10,20,30] ; State_2 = [4] ; States = {State_1,State_2}; % Structure for M = 1: 2 State_X = States{M}; % Perform some operations on State_X %... %... %Then Clear State_X end
|
Next
|
Last
Pages: 1 2 Prev: HELP!!!: How to plot a curve with direction in 3D? Next: Over Night Indexed Swaps OIS |