利用Matlab进行交集、并集等运算
Matlab是强大的数值分析与计算的软件,本例分享使用Matlab进行两个数集的交集、并集等运算。
intersect函数取交集
- 01
帮助文档: intersect Set intersection. C = intersect(A,B) for vectors A and B, returns the values common to the two vectors with no repetitions. C will be sorted. C = intersect(A,B,'rows') for matrices A and B with the same number of columns, returns the rows common to the two matrices. The rows of the matrix C will be in sorted order. [C,IA,IB] = intersect(A,B) also returns index vectors IA and IB such that C = A(IA) and C = B(IB). If there are repeated common values in A or B then the index of the last occurrence of each repeated value is returned. [C,IA,IB] = intersect(A,B,'rows') also returns index vectors IA and IB such that C = A(IA,:) and C = B(IB,:). [C,IA,IB] = intersect(A,B,'stable') for arrays A and B, returns the values of C in the same order that they appear in A. [C,IA,IB] = intersect(A,B,'sorted') returns the values of C in sorted order. If A and B are row vectors, then C will be a row vector as well, otherwise C will be a column vector. IA and IB are column vectors. If there are repeated common values in A or B then the index of the first occurrence of each repeated value is returned. [C,IA,IB] = intersect(A,B,'rows','stable') returns the rows of C in the same order that they appear in A. [C,IA,IB] = intersect(A,B,'rows','sorted') returns the rows of C in sorted order. In a future release, the behavior of the following syntaxes will change including: - occurrence of indices in IA and IB will switch from last to first - orientation of vector C - IA and IB will always be column index vectors - tighter restrictions on combinations of classes In order to see what impact those changes will have on your code, use: [C,IA,IB] = intersect(A,B,'R2012a') [C,IA,IB] = intersect(A,B,'rows','R2012a') If the changes in behavior adversely affect your code, you may preserve the current behavior with: [C,IA,IB] = intersect(A,B,'legacy') [C,IA,IB] = intersect(A,B,'rows','legacy') Examples: a = [9 9 9 9 9 9 8 8 8 8 7 7 7 6 6 6 5 5 4 2 1] b = [1 1 1 3 3 3 3 3 4 4 4 4 4 10 10 10] [c1,ia1,ib1] = intersect(a,b) % returns c1 = [1 4], ia1 = [21 19], ib1 = [3 13] [c2,ia2,ib2] = intersect(a,b,'stable') % returns c2 = [4 1], ia2 = [19 21]', ib2 = [9 1]' c = intersect([1 NaN 2 3],[3 4 NaN 1]) % NaNs compare as not equal, so this returns c = [1 3] Class support for inputs A and B, where A and B must be of the same class unless stated otherwise: - logical, char, all numeric classes (may combine with double arrays) - cell arrays of strings (may combine with char arrays) -- 'rows' option is not supported for cell arrays - objects with methods SORT (SORTROWS for the 'rows' option), EQ and NE -- including heterogeneous arrays derived from the same root class
- 02
简单示例:集合A: A = 1 2 3 4 5 6 7 8 9 10 集合B: > B=5:2:15 B = 5 7 9 11 13 15
- 03
求A∩B为:intersect(A,B)
setdiff补集运算
- 01
setdiff Set difference. C = setdiff(A,B) for vectors A and B, returns the values in A that are not in B with no repetitions. C will be sorted. C = setdiff(A,B,'rows') for matrices A and B with the same number of columns, returns the rows from A that are not in B. The rows of the matrix C will be in sorted order. [C,IA] = setdiff(A,B) also returns an index vector IA such that C = A(IA). If there are repeated values in A that are not in B, then the index of the last occurrence of each repeated value is returned. [C,IA] = setdiff(A,B,'rows') also returns an index vector IA such that C = A(IA,:). [C,IA] = setdiff(A,B,'stable') for arrays A and B, returns the values of C in the order that they appear in A. [C,IA] = setdiff(A,B,'sorted') returns the values of C in sorted order. If A is a row vector, then C will be a row vector as well, otherwise C will be a column vector. IA is a column vector. If there are repeated values in A that are not in B, then the index of the first occurrence of each repeated value is returned. [C,IA] = setdiff(A,B,'rows','stable') returns the rows of C in the same order that they appear in A. [C,IA] = setdiff(A,B,'rows','sorted') returns the rows of C in sorted order. In a future release, the behavior of the following syntaxes will change including: - occurrence of indices in IA will switch from last to first - orientation of vector C - IA will always be a column index vector - tighter restrictions on combinations of classes In order to see what impact those changes will have on your code, use: [C,IA] = setdiff(A,B,'R2012a') [C,IA] = setdiff(A,B,'rows','R2012a')
- 02
例如,求上例中在集合A中,不在集合B中的元素。命令为setdiff(A,B)
setxor去两个集合交集的补集
- 01
例如setxor(A,B)= setxor(A,B) ans = 1 2 3 4 6 8 10 11 13 15
union取两者的并集
- 01
union(A,B)= > union(A,B) ans = 1 2 3 4 5 6 7 8 9 10 11 13 15