How to change two variables at same time in a loop?

조회 수: 5 (최근 30일)
Pedro Vicente
Pedro Vicente 2018년 9월 22일
댓글: Star Strider 2018년 9월 22일
So i have something like:
for k = 1:nexecucoes
for i = 1:t*n
for v = 1:n
for j = 1:m
Pt(i,j,k) = 0.0116*r(v,j,k)*t*icorr;
end
end
end
end
And what i would like, is that when 'i' changes to i=2, 'v' changes also to v = 2.
It´s like, when i have i=1, v=1; i=2, v=2; i=3, v=1; i=4, v=2, and so on.
Can someone help me?

채택된 답변

Star Strider
Star Strider 2018년 9월 22일
I would define ‘v’ within the loop as:
v = (1.*(rem(i,2)==1)) + (2.*(rem(i,2)==0));
Your loops would then become:
for k = 1:nexecucoes
for i = 1:t*n
v = (1.*(rem(i,2)==1)) + (2.*(rem(i,2)==0));
for j = 1:m
Pt(i,j,k) = 0.0116*r(v,j,k)*t*icorr;
end
end
end
if I understand correctly what you want to do.
  댓글 수: 4
Pedro Vicente
Pedro Vicente 2018년 9월 22일
Yeah, this helped me! Thanks
Star Strider
Star Strider 2018년 9월 22일
As always, my pleasure!

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

추가 답변 (0개)

카테고리

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