Loops and subscripts. Is it possible to build a loop on, say "j" and refer to variables as "variable_1", "variable_2", "variable_last value of j"

조회 수: 4 (최근 30일)
Suppose that you have a loop on, say "j"c for j = 1:1:J; end; And you would like to have the following set of functions f(j) = c1_j + 2*c2_j
where j = 1, 2, 3,...., J
Is it possible to do this within a loop?
Thanks a lot, Matlab users.

채택된 답변

Star Strider
Star Strider 2016년 2월 2일
Yes, but it is considered to be very poor programming practice, so I won’t tell you how to do it. The best way is to refer to ‘c1_j’ and others as ‘c(i,j)’ where you iterate ‘i’ as well as ‘j’ as necessary in your code.
So:
f(j) = c1_j + 2*c2_j
becomes:
f(j) = c(1,j) + 2*c(2,j);

추가 답변 (1개)

Stephen23
Stephen23 2016년 2월 2일
편집: Stephen23 2019년 6월 19일
  댓글 수: 1
Cruz Angel Echevarria
Cruz Angel Echevarria 2016년 2월 2일
Wow! First of all, thanks a lot for your immediate and thorough answer! Second, I do realize that I am an absolutely inefficient Matlab programmer. I will try lo learn from users with more expertise. Once again, I really appreciate your help. Regards, Cruz

댓글을 달려면 로그인하십시오.

카테고리

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