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

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?

 Respuesta aceptada

Star Strider
Star Strider el 22 de Sept. de 2018
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 comentarios

Pedro Vicente
Pedro Vicente el 22 de Sept. de 2018
It's exactly what i want. thank you.
Tell me something, in this case i wanted v =1 or v = 2 because n = 2.
But if i have a n = 4, i would like to have i = 1 v =1, until i =4 v = 4, and then v comes back to 1.
Like:
i= 1 v=1
i =2 v=2
i=3 v=3
i=4 v=4
i=5 v=1
i=6 v=2 and so on
Do you know how can i make this?
This took some experimenting to develop.
I believe it will do what you want:
i = 1:15; % Create Vector To Test
v = ceil(rem(i,4.01)); % Calculate ‘v’
Check = [i; v]
Check =
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1 2 3 4 1 2 3 4 1 2 3 4 1 2 3
Pedro Vicente
Pedro Vicente el 22 de Sept. de 2018
Yeah, this helped me! Thanks
Star Strider
Star Strider el 22 de Sept. de 2018
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 22 de Sept. de 2018

Comentada:

el 22 de Sept. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by