Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird result using `==` operator in MATLAB

Tags:

matlab

Im getting a really weird result using == in MATLAB_R2009b on OS X. Example from the prompt:

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

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

like image 270
trolle3000 Avatar asked Dec 02 '10 23:12

trolle3000


1 Answers

In Matlab, strings are really just arrays of characters. So what you're really doing is comparing two arrays. This does an element-wise compare, i.e. character-by-character. So you could do:

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

but that would give a run-time error if the string length of class(s) was not 6. Much safer would be to do:

strcmp(class(s), 'double')

But what you should really be doing is:

isa(s, 'double')
like image 119
Oliver Charlesworth Avatar answered Sep 24 '22 13:09

Oliver Charlesworth