-
5 Comments
this returns "1" & not "true", but the validation for this cody problem is spurious since isequal(true,1) is actually true (when it should really throw an error).
Mark Whirdy is right
It is odd that matlab distinguishes between logical 1's and number 1's yet 1 and logical 1 are considered equal.
isequal([0 1],[false true])=1, yet if a is [1 2], a([0 1]) returns an error instead of 2, as it does with a([false true]).
@Brandon This appears to be an inconsistency, but it isn't really. true and false (more or specifically, the logical values 0 and 1) are equal to (in the sense of isequal) but distinguishable from the numerical (double) values 0 and 1. (And the concepts of equality and distinguishability are themselves distinct.)
Furthermore, MATLAB generally emphasizes usefulness to the user, and logical indexing is obviously very useful.
I didn't know I could not use the word "else" in the code until I submitted and passed all but the first of 8 tests. Also, I was not sure if they wanted the output to be a string ("true" or "false") or if 0 and 1 was sufficient.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!