필터 지우기
필터 지우기

nested for loop with different variable name

조회 수: 1 (최근 30일)
MUKESH KUMAR
MUKESH KUMAR 2018년 1월 12일
댓글: Walter Roberson 2018년 1월 18일
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에서 Whos에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by