problems with "if statement"
조회 수: 10 (최근 30일)
이전 댓글 표시
Hi everyone. I am beginning in Matlab and I have a problem.
My code follows:
for i=1:Nped
for j=1:length(t)
dist(i,j)=vp(i)*t(j);
for k=1:NH
if (dist(i,j)<=Lenght)
Findiv(i,:)=G(i)+alfadin(1)*G(i)*sin(2*pi*i*fp(i)*(t-Tentr(i))-phase(1));
else
Findiv(i,:)=0
end
end
end
For some reason, the loop :
if (dist(i,j)<=Lenght)
.... is not been well executed. I ploted the graphic of dist(1,j) vs t (time) and I verified that for a distance dist(1, j ) > lenght (where lenght= 20 m), the non-zero expression give by
Findiv(i,:)=G(i)+alfadin(1)*G(i)*sin(2*pi*i*fp(i)*(t-Tentr(i))-phase(1))
is not calculated (as showed in the second graph).

Does anyone could help me to solve this problem?
Thanks a lot =)
댓글 수: 4
Jan
2022년 3월 14일
In this version "phase(k)" appears. In the version you post in the comments below, it is "pahse(1)". This optimizes the cofusion fro the readers. We see 3 different codes now and all of them do, what they are doing. You are expecting something else, but we cannot guess why.
Use the debugger to examine whats going on. Set a breakpoint in the first line, then step through the code line by line. Of course, Matlab does exactly what it is instructed to do. The IF condition does work properly, so the only problem is that you expect something else.
The purpose if the "for k=1:NH" loop is still not clear and inside it Findiv is overwritten repeatedly. This cannot be useful.
채택된 답변
Jan
2022년 3월 13일
편집: Jan
2022년 3월 13일
The if command is not a loop.
What is the purpose if the "for k" loop? Its body does not depend on k, so you can omit this loop. Instead of setting the value of Finfiv explicitly to 0, you can pre-allocate also:
Findiv = zeros(Nped, 1);
for i = 1:Nped
tmp = G(i) + alfadin(1) * G(i) * ...
sin(2 * pi * i * fp(i) * (t - Tentr(i)) - phase(1));
for j = 1:length(t)
dist(i,j) = vp(i) * t(j);
if dist(i, j) <= Lenght
Findiv(i) = tmp;
break;
end
end
end
I've moved the calculation of G(i) + alfadin(1) *... out of the inner loop, because it does not depend on j and calculating it repeatedly is a waste of time. Therefore I've inserted a break also to leave the inner loop soon.
Maybe this is the problem: Findiv(i) is overwritten in each iteration of the loop over j, so it depends on the last value vp(i)*t(end) only.
댓글 수: 8
Torsten
2022년 3월 14일
Make that MATLAB recognizes t - otherwise the graphs will make no sense.
Still, the k-loop is superfluous - k is never referenced.
Image Analyst
2022년 3월 14일
You say that's what yhou got but that's not true. If you did get that, then you'd have gotten the error about t not being defined and you never would have gotten your plot. I'm really about to quit here. Either post the full code or I'm outta here. Perhaps Torsten will still be kind enough to help you.
추가 답변 (1개)
Voss
2022년 3월 14일
A couple of people have pointed out that the "for k=1:NH" loop is not necessary, but since it is still part of the code, can you check the value of NH?
If NH is less than 1 or NH is the empty array, then the "for k=1:NH" loop will execute zero times, leading to the if statement not being checked at all and Findiv not being calculated (but dist will be calculated because that is done outside the "for k=1:NH" loop).
This possibility is consistent with your observations of the values of dist and Findiv, if Findiv is pre-allocated with zeros.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Identification에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
