for loop only gives me one value
이전 댓글 표시
hello, i'm trying to write this code, however when I calculate xi it keep giving me one value, even though both alpha_g and ki_1 has 6 values. which then stops me from calculating yi with the following error
Index exceeds the number of array elements (1).
Error in Module_b (line 44)
yi(i)=xi(i).*ki_1(i);
this is the code:
for i=1:nc
if (min<alpha_g(i)&&alpha_g(i)<max)
for i=1:nc
xi(i)= z1./(1+alpha_g(i).*(ki_1(i)-1));
for i=1:nc
yi(i)=xi(i).*ki_1(i);
end
end
elseif alpha_g>=1
yi=z1;
else
xi=z1;
end
end
채택된 답변
추가 답변 (1개)
Torsten
2022년 2월 16일
You only set xi(1) in
for i=1:nc
xi(i)= z1./(1+alpha_g(i).*(ki_1(i)-1));
but refer to xi(1) up to xi(6) in the following loop
for i=1:nc
yi(i)=xi(i).*ki_1(i);
end
Furthermore, since there are nested loops in your code fragment, you cannot use the same loop index "i" every time.
댓글 수: 2
Eyad Alkhotani
2022년 2월 16일
Torsten
2022년 2월 16일
For this, you have to close the first for-loop:
for i=1:nc
xi(i)= z1./(1+alpha_g(i).*(ki_1(i)-1));
end
for i=1:nc
yi(i)=xi(i).*ki_1(i);
end
or simply
for i=1:nc
xi(i)= z1./(1+alpha_g(i).*(ki_1(i)-1));
yi(i)= xi(i).*ki_1(i);
end
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!