您的位置:首页 > 编程语言 > MATLAB

Matlab中的并交子集运算

2010-10-27 17:31 295 查看
以下函数都可以在Matlab的help中找到说明与例子。

1、并集:union

c = union(A, B) 返回矢量A与B的并集,A与B必须是数字或字符矢量或者字符元胞数组。结果是排序的。

c = union(A, B, 'rows') 当A与B是列数相同的矩阵时,返回A与B行的并集,如 A=[2,3,4],B=[1,2,3] 则返回[1,2,3 ; 2,3,4],返回结果是升序的。

[c, ia, ib] = union(...) also returns index vectors ia and ib such
that c = a(ia) b(ib), or for row combinations, c = a(ia,:) b(ib,:). If
a value appears in both a and b, union indexes its occurrence in b. If a
value appears more than once in b or in a (but not in b), union indexes
the last occurrence of the value

2、交集:intersect

c = intersect(A, B) 返回A与B的交集。

returns the values
common to both A and B. In set theoretic terms, this is A B. Inputs A
and B can be numeric or character vectors or cell arrays of strings. The
resulting vector is sorted in ascending order.

c = intersect(A, B, 'rows') when A and B are matrices with the same number of columns returns the rows common to both A and B.

[c, ia, ib] = intersect(a, b) also returns column index vectors ia
and ib such that c = a(ia) and c = b(ib) (or c = a(ia,:) and c =
b(ib,:)).

3、寻找唯一元素:unique

b = unique(A) 返回并剔除A中的重复元素。

returns the same
values as in A but with no repetitions. A can be a numeric or character
array or a cell array of strings. If A is a vector or an array, b is a
vector of unique values from A. If A is a cell array of strings, b is a
cell vector of unique strings from A. The resulting vector b is sorted
in ascending order and its elements are of the same class as A.

b = unique(A, 'rows') 返回A的不重复的行

[b, m, n] = unique(...) also
returns index vectors m and n such that b = A(m) and A = b(n). Each
element of m is the greatest subscript such that b = A(m). For row
combinations, b = A(m,:) and A = b(n,:).

4、寻找A与B中的不同元素:setdiff

c = setdiff(A, B) 返回在A中有但在B中没有的元素。

returns the values in A that
are not in B. In set theory terms, c = A - B. Inputs A and B can be
numeric or character vectors or cell arrays of strings. The resulting
vector is sorted in ascending order.

c = setdiff(A, B, 'rows'), when A and B are matrices with the same
number of columns, returns the rows from A that are not in B.

[c,i] = setdiff(...) also returns an index vector index such that c = a(i) or c = a(i,:)

例子:

如何把两矩阵比如

A=[1 4 6 3 5 9;7 5 6 8 2 5];B=[2 4 5;3 5 2];

中矩阵B中的每一列的两个元素若与矩阵A中有一列的两个元素相同,则把矩阵B中的这一列的两个元素除去(即去掉这一列)如:B中有一列为5 这一列的两个元素与A中其中一列的两个元素相同,则把 5 这一列从B中2 2去掉!

此时B=[2 4; 3 5]!!最后的结果B=[2;3]

解答:(setdiff(B',A','rows') )'

4、setxor:返回A与B的交集的取反

如:

>> A=[1,2,3,4],B=[2,5,9]

>> setxor(A,B)

ans =

1 3 4 5 9

c = setxor(A, B) returns the values
that are not in the intersection of A and B. Inputs A and B can be
numeric or character vectors or cell arrays of strings. The resulting
vector is sorted.

c = setxor(A, B, 'rows'), when A and B are matrices with the same
number of columns, returns the rows that are not in the intersection of A
and B.

[c, ia, ib] = setxor(...) also returns index vectors ia and ib such
that c is a sorted combination of the elements c = a(ia) and c = b(ib)
or, for row combinations, c = a(ia,:) and c = b(ib,:)

5、ismember:成员判定

tf = ismember(A, S) 返回与A相同大小的逻辑数组,当A中对应的元素可以在S中找到时,返回1,否则返回0

returns
a vector the same length as A, containing logical 1 (true) where the
elements of A are in the set S, and logical 0 (false) elsewhere. In set
theory terms, k is 1 where A S. Inputs A and S can be numeric or
character arrays or cell arrays of strings.

tf = ismember(A, S, 'rows'), when A and S are matrices with the same
number of columns, returns a vector containing 1 where the rows of A are
also rows of S and 0 otherwise. You cannot use this syntax if A or S is
a cell array of strings.

[tf, loc] = ismember(A, S, ...) returns index vector loc containing
the highest index in S for each element in A that is a member of S. For
those elements of A that do not occur in S, ismember returns 0.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: