当前位置: 首页 > backend >正文

matlab中unique的作用,matlab中的unique函数详解

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的作用是取重复值最后一次出现的角标。

http://www.xdnf.cn/news/11605.html

相关文章:

  • 思科模拟器Cisco Packet Tracer交换机的端口聚合配置
  • 热烈庆祝 中国x3d学习网:http://www.x3dcn.cn/正式开通
  • 必看!Python最新学习路线(2024最新版)
  • 移动端web开发
  • 【编程之美】一摞烙饼的排序
  • Mac新升级系统OS X El Capitan实用新功能快速指南
  • react-three/postprocessing库的中文对照总结
  • Android FactoryTest 流程
  • matlab 手指指尖检测,简单粗糙的指尖检测方法(FingerTips Detection) | 学步园
  • iphone开发论坛汇总:
  • 聊聊通用会员卡/通用打折卡
  • 十部值得一看的电影
  • CCProxy代理上网设置方法
  • 硬盘空间丢失的原因及预防方法(转)
  • 在美国,男 / 女卫生间(厕所)的正确称呼为什么?请用英语写出答案。
  • Oracle数据库的备份与恢复
  • 2023年最新QQ设置彩色昵称和动态头像
  • 跟踪百度竞价推广的效果2种方法
  • minecraft正版整合包服务器,我的世界1.7.2基佬整合包
  • SuperOneClick一键Root工具使用方法
  • 2013中国动作片《不二神探》高清BD电影下载
  • 第一次黑人,简直爽到不行!!
  • QQ第三方登录
  • 关于丢失msvcp71.dll的5个解决办法,msvcp71.dll丢失原因分析
  • “百度开放云编程马拉松”中国三大赛区获奖团队及作品新鲜出炉
  • ThinkPHP8完全教程(附案例源码和sql脚本)
  • SOA 案例研究:Web 2.0 SOA 场景
  • 什么是过期域名?做网站用过期域名好不好?
  • python 条件语句和基础数据类型
  • 【微信小程序】零基础快速入门