Help with intervals (new to MATLAB)

I'm extremely new to Matlab. This is something that our teacher showed us in class. He did not explain all that well, so I wondering if anyone can explain what is going on in lines 9, 10, 12, 14 and 15.
(I do not know what vpa means.)
In advance, thank you for taking your time and answering!

2 Comments

You admit you have no idea what vpa does. So why not start by reading the help for tools like vpa? After all, if you don't know what it does, would not the help tell you?
doc vpa
Ok, now I understand what vpa does. Thanks :) I'm still confused about what's happening in line 10. It'd be nice if you could explain.

Sign in to comment.

 Accepted Answer

When in doubt about a compound statement (made up of multiple pieces) in MATLAB take it one piece at a time.
c = c(a<= c & c<=b)
What gets done first? By operator precedence you do what's in the parentheses first.
a<=c & c<=b
So there are three parts there. Which one gets done next? <= is higher on the precedence list than & so next we figure out what this does:
a<=c
That's hopefully pretty self-explanatory. If it's not, look at the documentation for that operator.
doc <=
Similarly, this part is self-explanatory.
c<=b
Now what happens when you combine the results of those two parts with &? If you're not sure:
doc &
Now that you've figured out what the stuff inside the parentheses does, what does c(that stuff) mean? That's indexing.

2 Comments

Ok thanks a lot, dude! Really helped me out. So, if my understanding is correct, its to check if c belongs in the interval [a,b].
The part inside the parentheses does check if c is in the interval [a, b]. The parentheses themselves retrieves only those elements of c that are in that interval.
c = 1:10;
a = 4;
b = 6.5;
result = c(a <= c & c <= b)
result = 1×3
4 5 6

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2020b

Community Treasure Hunt

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

Start Hunting!