What is wrong with my if statement?
이전 댓글 표시
Hello all,
I'm not sure how to phrase my if statement so that when the value of y at the point where x is equal to dto, the statement will run.
Currently it stops at the beginning of the if statement so i know that thats where the problem lies.
Below is my code for the relevant parts:
if max(x)>=Ox %&& (y(x==max(x))<=Oy)
dto = find(x < Ox,1,'last')
tto = dto/v0x
if y(x==dto)<=Oy %%this is where it stops
xlim([0, Ox+Ot+0.5])
ylim([0, max(y)+0.5])
hold on
for i=1:length(tto)
plot(x(i),y(i),'ko')
pause(0.05)
end
elseif y(x==dto)>Oy
for i=1:length(tFinal)
plot(x(i),y(i),'ko')
pause(0.05)
end
end
end
Thanks in advance! :3
댓글 수: 9
Diaa
2021년 1월 8일
Can you put some values of x, y, ox, ot and oy that reproduce your problem?
Ryan Fedeli
2021년 1월 8일
^ This would be helpful.
Also I want to add that since you're using the find function, dto is defined as an index, so it's an integer. If x does not contain integers, this would be one reason why it never runs
Anon
2021년 1월 8일
Daniel Pollard
2021년 1월 8일
You've used logical indexing to get y where x=dto:
if y(x==dto)<=Oy %%this is where it stops
x is an array of values from 0 to 11.8817, and when I print dto, it returns 59, so the if statement isn't satisfied. So it goes to the elseif, where you say
for i=1:length(tFinal)
where, at least in the code you've given us, tFinal isn't defined. So it does nothing.
Ryan Fedeli
2021년 1월 8일
Well the find function works for any type of array. It returns the index (an integer number) of the desired value. So in the array:
p = [2.0 4.2 6.0 8.4 10.0];
index = find(p==6.0)
find will return 3. 3 is the place in the array p where p = 6.0.
So to find the value at the index given by find, all you need is
value = p(index)
Anon
2021년 1월 8일
Anon
2021년 1월 8일
Anon
2021년 1월 8일
답변 (0개)
카테고리
도움말 센터 및 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!