MATLAB Answers

Matlab - compare Cell-Array rows with mixed content

43 views (last 30 days)
Vincent
Vincent on 29 Aug 2011
Hi, I've searched already a while and I'm quite surprised that I couldn't find a nice and fast solution for this problem: I want to compare two cell-Arrays (per line) containing numbers and strings.
Example:
A = {'lol',2;'xd',2} B = {'lol',2}
shall return a logical array with
[1;0]
Has anyonane an Idea?

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 29 Aug 2011
A = cellfun(@num2str,A,'un',0);
B = cellfun(@num2str,B,'un',0);
out = arrayfun(@(i1)all(ismember(A(i1,:),B)),(1:size(A,1))')
or
out = all(ismember(A,B),2);
more
out = all([ismember(A(:,1),B(1)),ismember([A{:,2}]',B{2})],2)
more more
out = arrayfun(@(i1)isequal(A(i1,:),B),(1:size(A,1))');
or
out = cellfun(@(x)isequal(x,B),mat2cell(A,ones(size(A,1),1),2));
or use loop
for i1 = size(A,1):-1:1
out(i1,1) = isequal(A(i1,:),B);
end

More Answers (1)

Vincent
Vincent on 29 Aug 2011
thanks for your quick response! I wanted to avoid the first two lines (conversion) at all, but it seems like there's no other solution. For the third line, I've got another idea (as I don't get into this arrayfun(@(i1))-thing:
all(strcmp(repmat(A,size(B(:,1)),1),B),2)
edit @andrei: thanks for the first "or" - that's what I've searched for :)

Community Treasure Hunt

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

Start Hunting!