c=unique(a ) :返回与a相同的值,但没有重复元素。 结果向量按升序排序。
示例:
1 .将向量中的重复值过筛,将结果按升序排列
define a vector with a repeated value。
A=[9 2 9 5];
Find the unique values of A
c=unique(a ) )。
C=
2 5 9
2 .如果a是数组,则返回的是a不重复的行。 排列c的行按顺序排列。
Name={'Fred '; ' Betty '; ' ctdny '; ' jqdlm '; ' cjdwn'};
Age=[38; 43; 38; 40; 38 );
Height=[71; 69; 64; 67; 64 );
Weight=[176; 163; 131; 185; 131 );
a=table(age,Height,Weight,' RowNames ',Name ) ) )。
A=
智能微信
___ ______ ______
Fred 38 71 176
Betty 43 69 163
ctdny 38 64 131
jqdlm 40 67 185
cjdwn 38 64 131
Find the unique rows of A .
c=unique(a ) )。
C=
智能微信
___ ______ ______
ctdny 38 64 131
Fred 38 71 176
jqdlm 40 67 185
Betty 43 69 163
注:
行(cjdwn 38 64 131 )和(ctdny 38 64 131 )被重复删除。
根据第一个变量(年龄),然后是第二个变量(高度)进行排序,返回有秩序的行。
3 .获取非重复值及其下标
define a vector with a repeated value。
A=[9 2 9 5];
findtheuniquevaluesofaandtheindexvectorsiaandic,suchthatc=a(ia ) anda=c ) IC。
([C,ia,IC]=unique(a ) ) ) ) ) ) ) ) ) ) ) )。) ) )
C=
2 5 9
ia=
2
4
1
ic=
3
1
3
2
注(ia是指矩阵a中c中的元素)2)5)的位置; ic是矩阵c中a中的元素(9)的位置。
4 .获取矩阵中的非重叠行
定义a matrix with a repeated row。
A=[9 2 9 5; 九2九0; 9 )9) 5;
findtheuniquerowsofaandtheindexvectorsiaandic,suchthatc=a(ia, ) anda=c )集成电路, )。
([C,ia,IC]=unique(a,' rows ' ) ) )。
C=
9 2 9 0
9 2 9 5
ia=
2
1
ic=
2
1
2
注: c=a(ia, ),即a的哪两行构成c; a=c(IC, ),也就是说C的哪三行构成了A。
ia、ic表示行的下标。
5 .排除向量中的重复值,不对结果进行排序
usethesetorderargumenttospecifytheorderingofthevaluesinc。
特殊' stable ' ifyouwantthevaluesinctohavethesameorderasina。
A=[9 2 9 5];
([C,ia,IC]=unique(a,' stable ' ) ) )。
C=
9 2 5
ia=
1
2
4
ic=
1
2
1
3
注意:重复使用unique(a,’stable’)不会进行排序。 默认排序通常省略unique(a,“sorted”)和“sorted”。
6 .对于包含6.NaN(notanumbe )无限和非数值)的数列,unique函数是如何处理的呢?
定义a vector containing nan。
A=[5 5 NaN NaN];
Find the unique values of A
c=unique(a ) )。
C=
5 NaN NaN
注意: unique函数将NaN视为不同的元素。
7 .字符串单元格数组的非重叠项
Define a cell array of strings
A={'One ',' two ',' twenty-two ',' one ',' two'};
findtheuniquestringscontainedina。
c=unique(a ) )。
C=
' One' 'one' 'twenty-two' 'two '
注意: unique函数标识字符串是否相同,并区分大小写。
8 .末尾有空格的字符串单元格数组
定义字符串数组和字符串。 有些字符串有拖尾的空白。
A={'dog ',' cat ',' fish ',' horse ',' dog ',' fish '};
findtheuniquestringscontainedina。
c=unique(a ) )。
C=
' cat ' ' dog ' ' dog ' ' fish ' ' fish ' ' horse '
unique函数将末尾有空格的字符串数组视为不同的字符。 就像这里的‘fish’‘fish’一样。
9 .之前获得的要素下标都是要素最初出现的下标,通过legacy获得要素最后出现的下标。
use the ' legacy ' flagtopreservethebehaviorofuniquefromr 2012 bandpriorreleasesinyourcode。
findtheuniqueelementsofawiththecurrentbehavior。
A=[9 2 9 5];
([C1,ia1,IC1]=unique(a ) ) ) ) ) ) ) ) ) ) ) )。)
C1=
2 5 9
ia1=
2
4
1
ic1=
3
1
3
2
Find the unique elements of A,and preserve the legacy behavior。
([C2,ia2,IC2]=unique(a,' legacy ' ) ) ) ) ) ) ) ) ) ) ) ) )。
C2=
2 5 9
ia2=
2 4 3
ic2=
3 1 3 2
注:传统的作用是获取重复值最后出现的角点标记。