# nested for loop with different variable name

3 views (last 30 days)
MUKESH KUMAR on 12 Jan 2018
Commented: Walter Roberson on 18 Jan 2018
S is cell array of size 994*1 and each cell size in it is 24*24, price3(size 1*24), Bita(size 1*24), CONS_T3(size 1*24), I want to write Py1...Py994 like following this, how can we write in a loop....
% Py for S{1,1} its all rows
for i=1:24
Py1(i) = -S{1,1}(i,:)*(price3 + Bita.*(CONS_T3-S{1,1}(i,:))./100)';
end
% Py2 for S{2,1} for its all rows
for i=1:24
Py2(i) = -S{2,1}(i,:)*(price3 + Bita.*(CONS_T3-S{2,1}(i,:))./100)';
end
and Py3,Py4 ....so on.
Walter Roberson on 18 Jan 2018
In a duplicate copy of the question, the user attached

Walter Roberson on 12 Jan 2018
MUKESH KUMAR on 12 Jan 2018
Then can I do this...if i convert cell array to a matrix size(994*24,24) and then for i=1:24 find out Py1 and store it and for next 24 rows find out Py2 and from next 24 rows find out Py3 and so on ........... how can I do it in loop ?
Stephen23 on 12 Jan 2018
Edited: Stephen23 on 12 Jan 2018
" find out Py1 and store it and for next 24 rows find out Py2 and from next 24 rows find out Py3 and so on "
That is exactly how beginners write slow, buggy, insecure, obfuscated, complex code that is hard to debug. Your data is already conveniently in one array, which efficient and simple to use with vectorized code, access within loops, access using indexing, or by trivially passing to functions. You want to replace this simple code with something complex, buggy, and slow (because that is what magically accessing variable names means, no matter what method you use). Can you please explain why you suddenly need to make your code slow and inefficient?