Matlab中的並交子集運算

以下函數都可以在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.

發佈了34 篇原創文章 · 獲贊 13 · 訪問量 21萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章