nested for loop with different variable name

2 次查看(过去 30 天)
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
Walter Roberson 2018-1-12
  2 个评论
MUKESH KUMAR
MUKESH KUMAR 2018-1-12
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
Stephen23 2018-1-12
编辑:Stephen23 2018-1-12
" 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?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by