-
5 Comments
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.
Suggested Problems
-
Create a cell array out of a struct
2191 Solvers
-
Omit columns averages from a matrix
599 Solvers
-
401 Solvers
-
Is this triangle right-angled?
6118 Solvers
-
Double all elements in the array
1817 Solvers
More from this Author96
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!