comparing matices that contain numbers and NaNs

3 次查看(过去 30 天)
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
zz1=R(7,3:end);
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
zz==xx1
??? 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’
thanks
  1 个评论
Jan
Jan 2012-7-5
@Readers: It seems like anonet wants to compare cells, which contain numerical and string values.

请先登录,再进行评论。

采纳的回答

Andrei Bobrov
Andrei Bobrov 2012-7-5
编辑:Andrei Bobrov 2012-7-5
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=A;
B1=B;
A1(cellfun(@(x)all(isnan(x)),A)) = {0};
B1(cellfun(@(x)all(isnan(x)),B)) = {0};
out = isequal(A1,B1);
  4 个评论
antonet
antonet 2012-7-5
编辑:antonet 2012-7-5
can i omit
A1=A;
B1=B;
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)
thanks
Andrei Bobrov
Andrei Bobrov 2012-7-5
Hi Antonet! Thank you for "Accepted answer", but I mean that Jan's answer better.

请先登录,再进行评论。

更多回答(2 个)

Jan
Jan 2012-7-5
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.
  4 个评论
Teja Muppirala
Teja Muppirala 2012-7-5
On more recent releases, there is the ISEQUALN function so you don't have to write out ISEQUALWITHEQUALNANS

请先登录,再进行评论。


Kevin Claytor
Kevin Claytor 2012-7-4
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.
  3 个评论
Walter Roberson
Walter Roberson 2012-7-5
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
Jan 2012-7-5
@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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by