Help with intervals (new to MATLAB)
6 views (last 30 days)
Show older comments
Guruprasad Srikrishnan
on 11 Nov 2020
Commented: Steven Lord
on 13 Nov 2020

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
John D'Errico
on 11 Nov 2020
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
Accepted Answer
Steven Lord
on 12 Nov 2020
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)
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
Steven Lord
on 13 Nov 2020
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)
More Answers (0)
See Also
Categories
Find more on Entering Commands 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!