for loop storing the same variable
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Reinhardt RADING
le 8 Août 2022
Commenté : Steven Lord
le 8 Août 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.
0 commentaires
Réponse acceptée
Matt J
le 8 Août 2022
Modifié(e) : Matt J
le 8 Août 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 commentaires
Steven Lord
le 8 Août 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.
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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!