What is wrong with my if statement?
Show older comments
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 Comments
Diaa
on 8 Jan 2021
Can you put some values of x, y, ox, ot and oy that reproduce your problem?
Ryan Fedeli
on 8 Jan 2021
^ 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
on 8 Jan 2021
Daniel Pollard
on 8 Jan 2021
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
on 8 Jan 2021
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
on 8 Jan 2021
Anon
on 8 Jan 2021
Anon
on 8 Jan 2021
Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!