Matlab - compare Cell-Array rows with mixed content

3 次查看(过去 30 天)
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?

采纳的回答

Andrei Bobrov
Andrei Bobrov 2011-8-29
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

更多回答(1 个)

Vincent
Vincent 2011-8-29
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 :)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by