MATLAB中查找两个不同维度的矩阵中相同或者不同元素的位置
intersect
Find set intersection of two vectors
Syntax
c = intersect(A, B)
c = intersect(A, B, ‘rows’)
[c, ia, ib] = intersect(a, b)
Description
c = intersect(A, B) returnsthe values common to both A and B.In set theoretic terms, this is A ∩ B.Inputs A and B can be numericor character vectors or cell arrays of strings. The resulting vectoris sorted in ascending order.
c = intersect(A, B, ‘rows’) when A and B arematrices with the same number of columns returns the rows common toboth A and B. MATLAB ignoresthe rows flag for all cell arrays.
[c, ia, ib] = intersect(a, b) alsoreturns column index vectors ia and ib suchthat c = a(ia) and b(ib) (or c= a(ia,:) and b(ib,:)).
Tips
Because NaN is considered to be not equal to itself, it is neverincluded in the result c.
Examples
A = [1 2 3 6]; B = [1 2 3 4 6 10 20];
[c, ia, ib] = intersect(A, B);
disp([c; ia; ib])
1 2 3 6
1 2 3 4
1 2 3 5