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!