for loop storing the same variable

1 次查看(过去 30 天)
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.

采纳的回答

Matt J
Matt J 2022-8-8
编辑:Matt J 2022-8-8
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 个评论
Steven Lord
Steven Lord 2022-8-8
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by