Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

comparing matices that contain numbers and NaNs

Asked by antonet on 4 Jul 2012
 I have 2 cell matrices R and W and I want to check  if they contain the same elements. Specifically, I want to compare R which is obtained from

[N,T,R] = xlsread( filename); %

 with another matrix W.Both matrices are cell matrices and can also contain NaN entries.

My goal: If my calculations are correct then

          kk=W(7 ,3:end); should contain the same elements with


or zz2=R(8,3:end);

So to test this I do something like

if zz==xx1 | zz== xx2

'OK' else 'Not OK' EndThe problem is that


??? Undefined function or method 'eq' for input arguments of type 'cell'.

So I use cell2mat(zz)==cell2mat(xx1) and it works.

But if zz and xx1 contain NaN then for some reason I cannot use the cell2mat() approach

I am looking for a general approach that will enable me to do that comparison whether I have NaN and numbers or only numbers’


1 Comment

Jan Simon on 5 Jul 2012

@Readers: It seems like anonet wants to compare cells, which contain numerical and string values.




No products are associated with this question.

3 Answers

Answer by Andrei Bobrov on 5 Jul 2012
Edited by Andrei Bobrov on 5 Jul 2012
Accepted answer

variant of comparison cell arrays (edit)

A = {...
[84]    [ 93]    [   109]
[99]    [NaN]    [    62]
[29]    [123]    'dgdgdg'};
B = {...
[84]    [ 93]    [   109]
[99]    [NaN]    [    62]
[29]    [123]    'dgdgdg'};
A1(cellfun(@(x)all(isnan(x)),A)) = {0};
B1(cellfun(@(x)all(isnan(x)),B)) = {0};
out = isequal(A1,B1);


Andrei Bobrov on 5 Jul 2012

yas, thank.

antonet on 5 Jul 2012

can i omit



so as to simlpy have

 A = {...
[84]    [ 93]    [   109]
[99]    [NaN]    [    62]
[29]    [123]    'dgdgdg'};
B = {...
[84]    [ 93]    [   109]
[99]    [NaN]    [    62]
[29]    [123]    'dgdgdg'};

A(cellfun(@(x)all(isnan(x)),A)) = {0};

B(cellfun(@(x)all(isnan(x)),B)) = {0};

out = isequal(A,B)


Andrei Bobrov on 5 Jul 2012

Hi Antonet! Thank you for "Accepted answer", but I mean that Jan's answer better.

Andrei Bobrov
Answer by Jan Simon on 5 Jul 2012
C1 = {'String1', 23, -17, NaN,  1:29);
C2 = {'String2', 24, -17.3, 21, 1:30);
C3 = {'String1', 23, -17, NaN,  1:29);
% |C1==C2| fails!
isequalwithequalnans(C1, C2)
isequalwithequalnans(C1, C3)

I cannot test this currently. When NaNs should be assumed to be different, isequal would do it.


Teja Muppirala on 5 Jul 2012

On more recent releases, there is the ISEQUALN function so you don't have to write out ISEQUALWITHEQUALNANS

antonet on 5 Jul 2012

what is +1 means?

Jan Simon
Answer by Kevin Claytor on 4 Jul 2012

My guess from searching the help docs for 'cell' gives;

m = cell2mat(c) converts a multidimensional cell array c with contents of the same data type into a single matrix, m.


antonet on 4 Jul 2012

I forgot to tell you that it contains also string variables and if i type


i get :

Error using cell2mat (line 46) All contents of the input cell array must be of the same data type.

Walter Roberson on 5 Jul 2012

What do you want to have happen in the locations that contain strings? It is not possible for a numeric array to contain strings.

Jan Simon on 5 Jul 2012

@antonet: Please provide the necessary information in the original question, because readers in this forum will read it at first. Therefore you can edit the question.

The code you have posted is hard to read. Please read the instructions about code formatting again.

Kevin Claytor

Contact us