Need to run two counters in a foor loop

4 visualizaciones (últimos 30 días)
Adil Ali
Adil Ali el 1 de Mayo de 2021
Respondida: Steven Lord el 1 de Mayo de 2021
Here is my problem.
I have two lists of the same length = A. The lists are P and T.
I anticipate to be running a for loop (for ls=1:A) embedded in another foor loop (for i = 1:ls).
Below is what I would like to get when the loops are running:
ls = 1
i = 1
P(1) + T(1);
ls = 2
i = 1
P(1) + T(2);
i = 2
P(2) + T(1);
ls = 3
i = 1
P(1) + T(3);
i = 2
P(2) + T(2);
i = 3
P(3) + T(1);
ls = 4
i = 1
P(1) + T(4);
i = 2
P(2) + T(3);
i = 3
P(3) + T(2);
i = 4
P(4) + T(1);
....... and so on until the lenght of A
I need help in making the counter with T go backwards from the value of ls. Create a dynamic vector in the loop is not a good idea
for ls=1:A
K = ls:-1:1
for i=1:ls
P(i)+T(K(i))
end
end
Id appreciate for someone's help as I have spent 3 hours trying to come up with a solution for this and got to no where. Maybe a formula instead of the "????" below.
for ls=1:A
for i=1:ls
P(i)+T(????)
end
end
Thank you.

Respuestas (3)

dpb
dpb el 1 de Mayo de 2021
Editada: dpb el 1 de Mayo de 2021
If you describe the real problem to be solved instead, it'll lead to more efficient getting to the right answer, but if the result is to simply sum the two variables in obverse order, you don't need loop at all --
S=P+flipud(T);
  1 comentario
Adil Ali
Adil Ali el 1 de Mayo de 2021
That is right. My apologies. Here is the acutal problem.
I am simulating emittion of laser pulses. I create pre allocated array of zeros
pulses=zeros(length(t),4); %% need other columns for other information but the first column needs to be distance travelled by each pulse at time = t.
Then i run my laser for time = LaserOnTime and the pulses are emitted (one after the other) at PRF = 500kHz. so I create an array
t = 1:1/PRF:LaserOnTime;
for ls=1:length(t)
k = ls:-1:1;
for i=1:(ls)
if(pulses(i,1)<=2*Altitude)%%increment the distance of pulse until 2*alt is %achieved
pulses(i,1) = pulses(i,1) + SpeedOfLight*t(k(i)); %%increment the %distance of each pulse
end
end
end
This works but k=ls:-1:1 changes its size at each counter and this is not the most idea solution.
Thank you.

Iniciar sesión para comentar.


David Fletcher
David Fletcher el 1 de Mayo de 2021
One way of recreating your original sequence via the loops would be:
A=ones(1,5);
for iterator=1:length(A)
sub=0;
fprintf('outer loop: %d\n',iterator)
for ls=1:iterator
fprintf('P(%d) + T(%d)\n',ls,iterator-sub)
sub=sub+1;
end
iterator=iterator+1;
end
This gives:
outer loop: 1
P(1) + T(1)
outer loop: 2
P(1) + T(2)
P(2) + T(1)
outer loop: 3
P(1) + T(3)
P(2) + T(2)
P(3) + T(1)
outer loop: 4
P(1) + T(4)
P(2) + T(3)
P(3) + T(2)
P(4) + T(1)
outer loop: 5
P(1) + T(5)
P(2) + T(4)
P(3) + T(3)
P(4) + T(2)
P(5) + T(1)

Steven Lord
Steven Lord el 1 de Mayo de 2021
F = 3;
S = 4;
for first = 1:F
fprintf("Counting up, first is %d\n", first)
for second = S:-1:1
fprintf("Counting down, second is %d\n", second)
fprintf("The equivalent counting up inner counter would be %d\n", S-second+1)
end
fprintf(" \n")
end
Counting up, first is 1
Counting down, second is 4
The equivalent counting up inner counter would be 1
Counting down, second is 3
The equivalent counting up inner counter would be 2
Counting down, second is 2
The equivalent counting up inner counter would be 3
Counting down, second is 1
The equivalent counting up inner counter would be 4
Counting up, first is 2
Counting down, second is 4
The equivalent counting up inner counter would be 1
Counting down, second is 3
The equivalent counting up inner counter would be 2
Counting down, second is 2
The equivalent counting up inner counter would be 3
Counting down, second is 1
The equivalent counting up inner counter would be 4
Counting up, first is 3
Counting down, second is 4
The equivalent counting up inner counter would be 1
Counting down, second is 3
The equivalent counting up inner counter would be 2
Counting down, second is 2
The equivalent counting up inner counter would be 3
Counting down, second is 1
The equivalent counting up inner counter would be 4

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by