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.
댓글 수: 0
채택된 답변
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
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.
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 Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!