如何提供两个列表之间的差异列表

How to provide the list of diff between 2 lists

我有2个细胞

exp = {'test','tat','toto'};
act = {'test','toto','tat','pto'};

并想检查这些列表是否相等。当它们没有相同数量的元素时,我如何提供差异列表?

简而言之,使用setdiff;请参阅文档,其中有一个关于您想要做什么的示例。

编辑

需要对 setdiff 进行说明。根据文档:

C = setdiff(A,B) returns the data in A that is not in B.

这句话一定要准确理解:它returnsA的数据不在B中。所以setdiff与其参数不对称!对于你的问题,如果A的所有元素都在B中,即使B再大,结果集也是空的。

为了得到两个集合之间的差异,换句话说,你想要一个关于它们参数的对称函数,Matlab 提供了另一个函数,setxor:

C = setxor(A,B) returns the data of A and B that are not in their intersection (the symmetric difference).

您可以使用 setdiff 命令。

exp = {'test', 'tat', 'toto'}; 
act = {'test', 'toto', 'tat', 'pto'};
diff = setdiff(exp, act);

您可以在文档中找到它: http://www.mathworks.com/help/matlab/ref/setdiff.html?refresh=true

对于 setdiff 输入的顺序很重要。 setdiff(A,B) return 是 A 中不在 B 中的条目列表 只有 ,它不 return 那些B 中的条目不在 A.

exp = {'test','tat','toto'};
act = {'test','toto','tat','pto'}
setdiff(exp,act); % empty because there is nothing in exp which isn't in act
setdiff(act,exp); %returns 1 x 1 cell, 'pto'.

而是使用 setxor(A,B),其中 return 的值不在 AB 的交集内。输入顺序无关紧要:

exp = {'test','tat','toto','pta'};
act = {'test','toto','tat','pto'};
setxor(exp,act) % returns 'pta','pto'