I need some help with logical operators and the find function...

20 views (last 30 days)
Hello, I'm trying to find the times (t) where, given some equations for projectile launch, the velocity and height are within certain ranges, simultaneously. However, I am stuck at the point where I perform the logical/relational operations (I am assuming while loops?
Thank you for the help
  1 Comment
Cedric
Cedric on 14 Apr 2013
Edited: Cedric on 14 Apr 2013
I don't really mind if you change your name, M.I., but don't overwrite/delete former questions; it is not respectful to the persons who take time to answer. Instead, start a new thread if you really have a new question.

Sign in to comment.

Accepted Answer

Cedric
Cedric on 14 Apr 2013
Edited: Cedric on 14 Apr 2013
You compute all elements of V and H, so they are fully defined at the moment you want to select relevant times. You can use logical indexing based on the output of relational operators (involving V, H, and threshold values), and avoid this iterative process that you are trying to implement with the nested WHILE loop.
Here is an example that you can then apply/adapt to your case: assume that you have t, x, and y, and you want to identify y that are associated with t>4 and x>=1.
>> t = 1 : 10 % Example of t.
t =
1 2 3 4 5 6 7 8 9 10
>> x = (t-5).^2 % Example of x.
x =
16 9 4 1 0 1 4 9 16 25
>> y = 101:110 % Example of y.
y =
101 102 103 104 105 106 107 108 109 110
Now we can build a vector of logicals whose elements are 1 (true) where both conditions (on t and x) are true, as follows:
>> id = t > 4 & x >= 1
id =
0 0 0 0 0 1 1 1 1 1
>> class(id) % Check that class/type is logical (=boolean).
ans =
logical
If you look where 1's are, you can see that it corresponds to places where t>4 and x>=1 simultaneously.
Now this vector of logicals, id, can be used to index any array along a dimension with same length as id. In particular, we can use it to indexselect elements of y:
>> y(id)
ans =
106 107 108 109 110
If you check, these are the relevant values of y (that correspond to elements of t and x which verify both conditions).
As you can see, there is no loop here; we did it in a "vector" way. We also avoided using FIND. If you look at what you are doing when you use FIND, you will realize that you would have done something like:
>> pos = find(t > 4 & x >= 1)
pos =
6 7 8 9 10
>> y(pos)
ans =
106 107 108 109 110
but you can see that the argument of FIND is indeed the vector of logicals that we named id above (FIND returns in fact just the position of the non-zero/false elements of its argument). You can then use these positions to index y (this is called subscript or linear indexing), but you get the same elements using directly the vector of logicals (called logical indexing), so there is no point in making the extra step of using FIND.
  2 Comments
Lemmiwinks
Lemmiwinks on 14 Apr 2013
Thank you so much, that is an intuitive way of thinking about it. I finished the rest successfully, thanks for the help! I appreciate it

Sign in to comment.

More 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!