problems with "if statement"

3 次查看(过去 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 个评论
Igor Braz Gonzaga
Igor Braz Gonzaga 2022-3-13
I'm sorry, I copied the test code. The correct follows:
for i=1:Nped
Findiv=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);
for k=1:NH
if (dist(i,j)<=20) %&(t(j)>=Tentr(i))
Findiv(i,:)=G(i)+alfadin(k)*G(i)*sin(2*pi*i*fp(i)*(t-Tentr(i))-phase(k));
else
Findiv(i,:)=0;
end
end
%Ft(:)=Ft(:)+Findiv(i,:)
end
Jan
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
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
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
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
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.

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by