How to compare variables without if statement
5 views (last 30 days)
Show older comments
new2matlab on 2 Dec 2019
I'm looking to verify that two variables produce the same (or different, then an error will occur) result. I'm looking to find a quick way to do this without utilizing if statements.
Edited: dpb on 3 Dec 2019
Of course, you can make things more interesting by wrapping the assert inside a try...catch construct...
>> a=3;b=4; % unequal variables
assert(a==b,'') % check equality, no message output
disp('Unequal') % do whatever on error here
Of course, what this gains over simply writing a straightforward if...else...end construct is difficult to see without context.
What's to explain?
> help assert
assert Generate an error when a condition is violated.
assert(EXPRESSION) evaluates EXPRESSION and, if it is false, displays the
error message 'Assertion Failed'.
assert(EXPRESSION, ERRMSG) evaluates EXPRESSION and, if it is false,
displays the character vector contained in ERRMSG. ...
See the doc for all the gory details.
You gave no klews as to what you expect to be able to do...
Find more on Shifting and Sorting Matrices 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!