function [b,ndx,pos] = unique(a,flag1,flag2)
% UNIQUE Set unique.
% B = UNIQUE(A) for the array A returns the same values as in A but
% with no repetitions. B will also be sorted. A can be a cell array of
% strings.
%
% UNIQUE(A,'rows') for the matrix A returns the unique rows of A.
%
% [B,I,J] = UNIQUE(...) also returns index vectors I and J such
% that B = A(I) and A = B(J) (or B = A(I,:) and A = B(J,:)).
%
% [B,I,J] = UNIQUE(...,'first') returns the vector I to index the
% first occurrence of each unique value in A. UNIQUE(...,'last'),
% the default, returns the vector I to index the last occurrence.
取集合的单值元素
函数格式 b = unique (a) %取集合a的不重复元素构成的向量
b = unique (A,'rows') % 返回A中不同行向量组成的矩阵
[b,i,j] = unique (…) %i、j体现b中元素在原向量(矩阵)中的位置
例
>> A=[1 1 2 2 4 4 6 4 6]
A =
1 1 2 2 4 4 6 4 6
>> B=unique(A)
B =
1 2 4 6
A =
1 1 2 2 4 4 6 4 6
>> [B,i,j]=unique(A)
B =
1 2 4 6
i =
2 4 8 9
j =
1 1 2 2 3 3 4 3 4
例
>> A=[1 2 2 4;1 1 4 6;1 1 4 6]
A =
1 2 2 4
1 1 4 6
1 1 4 6
>> [B,i,j]=unique(A,'rows') %A中A(1,:)与A(2,:)不同,所以返回这2个向量组成的矩阵。A(2,:)和A(3,:)相同,
B =
1 1 4 6
1 2 2 4
i =
3
1
j =
2
1
1
B=A(i,:);
A=B(j,:);