nested for loop with different variable name

1 vue (au cours des 30 derniers jours)
MUKESH KUMAR
MUKESH KUMAR le 12 Jan 2018
Commenté : Walter Roberson le 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.

Réponse acceptée

Walter Roberson
Walter Roberson le 12 Jan 2018
  2 commentaires
MUKESH KUMAR
MUKESH KUMAR le 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
Stephen23 le 12 Jan 2018
Modifié(e) : Stephen23 le 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?

Connectez-vous pour commenter.

Plus de réponses (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by