我需要在MATLAB中比较两个表的内容,更准确地说是两列(每个表一列),以查看第一列的每个元素,如果第二列中有相等的元素.
我应该使用for循环还是现有的MATLAB函数可以执行此操作?
%# create two arrays
A = [1,2;1,3;2,5;3,3];
B = [2,2;1,3;1,5;3,3];
%# compare the second column of A and B, and check if the comparison is `true` for all elements
all(A(:,2)==B(:,2))
ans =
1
Run Code Online (Sandbox Code Playgroud)
如果订单不重要且所有元素都是唯一的,请使用ismember
all(ismember(A(:,1),B(:,1))
ans =
1
Run Code Online (Sandbox Code Playgroud)
如果订单不重要,并且有重复,请使用sort
all(sort(A(:,1))==sort(B(:,2)))
ans =
0
Run Code Online (Sandbox Code Playgroud)