Amazing result using the `==` operator in MATLAB

I get a really strange result using ==in MATLAB_R2009b in OS X. An example from the prompt:

s =
     2
>> class(s)
ans =
double
>> class(s) == 'double'
ans =
     1     1     1     1     1     1

Six times yes? Can someone explain this || offer a solution?

+5
source share
1 answer

In Matlab, strings are just arrays of characters. So what you really do is compare two arrays. This has an elementary comparison, that is, a characteristic character. So you can do:

all(class(s) == 'double')

but this will give a runtime error if the line length class(s)was not 6. It would be much safer to do:

strcmp(class(s), 'double')

But you really have to do:

isa(s, 'double')
+15
source

All Articles