for loop storing the same variable

1 Ansicht (letzte 30 Tage)
Reinhardt RADING
Reinhardt RADING am 8 Aug. 2022
Kommentiert: Steven Lord am 8 Aug. 2022
I have a for loop as below:
I is intensity of length 1000 (constant)
noise is a random white noise of length 1000
demodulated_power_vector1=zeros(floor(length(I)/4),4);
demodulated_power_vector2=zeros(floor(length(I)/4),4);
demodulated_power_vector3=zeros(floor(length(I)/4),4);
demodulated_power_vector4=zeros(floor(length(I)/4),4);
for ii=0:floor(length(I)/4)
for ss= 0:floor(length(noise)/4)
demodulated_power_vector1(:,1)=(I(1+ii)/2)*(1+cos(Dphi+noise(1+ss)+pi-alphaa));
demodulated_power_vector2(:,2)=(I(2+ii)/2)*(1+cos(Dphi+noise(2+ss)+pi+alphaa));
demodulated_power_vector3(:,3)=(I(3+ii)/2)*(1+cos(Dphi+noise(3+ss)-pi+alphaa));
demodulated_power_vector4(:,4)=(I(4+ii)/2)*(1+cos(Dphi+noise(4+ss)-pi-alphaa));
end
end
Why is the for loop storing the same variable? I'm getting the same answer. for example, the 250 variables stored in demodulated_power_vector1 are the same.
I dont know what i'm doing wrong. Kindly assist.

Akzeptierte Antwort

Matt J
Matt J am 8 Aug. 2022
Bearbeitet: Matt J am 8 Aug. 2022
Because in every pass through the loop, you assign the result to the same location. Perhaps you meant,
demodulated_power_vector1(ii,1)=(I(1+ii)/2)*(1+cos(Dphi+noise(1+ss)+pi-alphaa));
  3 Kommentare
Matt J
Matt J am 8 Aug. 2022
See my suggestion above.
Steven Lord
Steven Lord am 8 Aug. 2022
Since this assignment is inside two for loops @Reinhardt RADING probably wants to use both ii and ss to specify the "target" section of the demodulated power vector into which MATLAB should assign the output of the calculations to the right of the equals sign. But as a matter of fact, the suffixes (1, 2, 3, and 4) that distinguish the four demodulated power vectors probably also should be indices, making demodulated_power_vector a 3-dimensional array.
See this Answers post for some motivation behind moving the suffixes into actual indices.
demodulated_power_vectors = zeros(floor(length(I)/4),4, 4);
With appropriate calls to reshape it may be possible to eliminate one or both of the for loops.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by