matlab中的unique函数详解
2017-10-28 10:20
288 查看
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';'Bob';'George';'Jane'}; 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 = Age Height Weight ___ ______ ______ Fred 38 71 176 Betty 43 69 163 Bob 38 64 131 George 40 67 185 Jane 38 64 131 Find the unique rows of A. C = unique(A) C = Age Height Weight ___ ______ ______ Bob 38 64 131 Fred 38 71 176 George 40 67 185 Betty 43 69 163
注:
行(Jane 38 64 131)与 (Bob 38 64 131 )重复,被删除。
根据第一个变量(年龄),然后是第二个变量(高度)进行排序,返回一个有序的行。
3.获得非重复值及其下标
Define a vector with a repeated value. A = [9 2 9 5]; Find the unique values of A and the index vectors ia and ic, such that C = A(ia) and A = C(ic). [C, ia, ic] = unique(A) C = 2 5 9 ia = 2 4 1 ic = 3 1 3 2
注:ia是指C中元素(2 5 9)在矩阵A中的位置;ic是指A中元素(9 2 9 5)在矩阵C中的位置。
4.获得矩阵中非重复的行
Define a matrix with a repeated row. A = [9 2 9 5; 9 2 9 0; 9 2 9 5]; Find the unique rows of A and the index vectors ia and ic, such that C = A(ia,:) and A = C(ic,:). [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.筛除向量中的重复值,产生的结果不排序
Use the setOrder argument to specify the ordering of the values in C. Specify 'stable' if you want the values in C to have the same order as in A. 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.对于含有NaN(Not a Numbe:无穷与非数值)的数列,unique函数将如何处理呢?
Define 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'}; Find the unique strings contained in A. C = unique(A) C = 'One' 'one' 'twenty-two' 'two'
注:unique函数可识别字符串相同与否,分大小写。
8.带拖尾空白的字符串元胞数组
定义一个字符串数组,一个字符串,其中一些字符串有拖尾的空白。
A = {'dog','cat','fish','horse','dog ','fish '}; Find the unique strings contained in A. C = unique(A) C = 'cat' 'dog' 'dog ' 'fish' 'fish ' 'horse'
unique函数将带拖尾的空白的字符串数组视为不同的字符。如这里的’fish’ ‘fish ‘。
9.之前获得元素下标都是元素第一次出现的下标,用legacy获取元素最后一次出现的下标。
Use the 'legacy' flag to preserve the behavior of unique from R2012b and prior releases in your code. Find the unique elements of A with the current behavior. 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
注:legacy的作用是取重复值最后一次出现的角标。
阅读更多相关文章推荐
- Matlab padarray函数详解
- Matlab之graythresh()函数详解
- matlab最小二乘法数据拟合函数详解
- matlab 对于图像操作函数的详解
- MATLAB view函数详解
- Matlab中bsxfun和unique函数解析
- Matlab中plot函数及legend函数详解
- Matlab中pdist 函数详解(各种距离的生成)
- Matlab注释函数legend详解
- Matlab中图例注释函数legend详解
- Matlab textread函数详解
- jQuery数组($.each,$.grep,$.map,$.merge,$.inArray,$.unique,$.makeArray)处理函数详解
- matlab函数定义详解
- Query数组($.each,$.grep,$.map,$.merge,$.inArray,$.unique,$.makeArray)处理函数详解
- matlab unique函数
- matlab 各种函数详解(收集)
- Matlab xcorr函数详解
- Matlab中pdist 函数详解(各种距离的生成)
- jQuery数组($.each,$.grep,$.map,$.merge,$.inArray,$.unique,$.makeArray)处理函数详解
- Matlab中保持原来数值顺序的unique函数实现