Repeating for loop N times

조회 수: 8 (최근 30일)
Sohini Bhattacharjee
Sohini Bhattacharjee 2022년 4월 22일
댓글: Jon 2022년 4월 25일
I want to repeat the below written set of code 11 times, such as the loop runs from 1 to 500 from 11 times. How do I do that? please help!
c=1:100:500;
L_vec=zeros(1,length(c));
for ii=1:length(c);
L_vec(ii)=(H/((1/D)+(K/J)))*exp(-c(ii)/D)
end

답변 (2개)

Cris LaPierre
Cris LaPierre 2022년 4월 22일
Sounds like you need to put all your code inside a 2nd for loop

Jon
Jon 2022년 4월 22일
편집: Jon 2022년 4월 22일
You need to use a nested for loop, so something like this, where I have assumed that H,D,K,J are length, numLoops vectors. You can adapt according to your actual situation.
numLoops = 11
c=1:100:500;
L_vec=zeros(num,length(c));
for j = 1:numLoops
for ii=1:length(c);
L_vec(j,ii) = (H(j)/((1/D(j))+(K(j)/J(j))))*exp(-c(ii)/D(j));
end
end
  댓글 수: 2
Sohini Bhattacharjee
Sohini Bhattacharjee 2022년 4월 23일
Hello. Thanks for your response. However I am getting an error message, such as "Index exceeds the number of array elements (1)". Can you please help?
Jon
Jon 2022년 4월 25일
It is most likely that one or more of your variables, H ,D, K, J, D are not length 11 (the number of outer loops) vectors. In fact it looks like at least one of them is just a scalar (single element) as that is what the error message is saying.
To help you further I would need to see your complete code, and also please copy and paste the entire error message.

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

카테고리

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