首页 > 编程知识 正文

matlab访问矩阵元素,去重函数unique

时间:2023-05-03 17:05:55 阅读:107676 作者:4196

%Unique

%1。

%c=unique(a ) returns the same data

as in A,but with no repetitions .

a=[ 1,1; 二、二; 三、三; 四、四; 五、四; 五、三; 五六; 七、四; 八、四; 八五; 九、四);

c=unique(a );

%

%2。

%c=unique(a,' rows ' ) ) treats each

rowofaasasingleentityandreturnstheuniquerowsofa.the

rowsofthearraycareinsortedorder

b=[ 1,1; 二、二; 三、三; 四、四; 五、三; 五、三; 五六; 七、四; 八、四; 八五; 九、四);

c=unique(b,' rows ' ) ) )。

%结果:

% C=

%

% 1 1

% 2 2

% 3 3

% 4 4

% 5 3

% 5 6

% 7 4

% 8 4

% 8 5

% 9 4

%

%

c=unique(b,' rows ' )用于比较行,只有当两行中的数据完全相同时,它们才会被视为相同。 例如,如果5、3在上述b中出现两次,则视为重复值

%

%

要在此比较列,可以使用unique(b,' rows ' )进行替换

% 3。

% [C,ia,IC]=unique(a ) also returns

索引vectors ia and IC .

% If A is a vector,thenc=a(ia ) ) ) )。

anda=c (集成电路)。

% If A is a matrix or array,then C

=a(ia ) anda ) : )=c ) IC )。

% If A is a table,thenc=a(ia, ) )。

anda=c(IC, ) )。

a=[ 1,1; 二、二; 三、三; 四、四; 五、三; 五、三; 五六; 七、四; 八、四; 八五; 九、四);

[C,ia,IC]=unique(a );

%返回部分省略

%这里c是a的元素,但只写一次

%ia,代表性的. c位置,C=

a(ia )可以得到c

%ic表示a的位置,A=

c ) IC )可以得到a,但必须注意,其中a只是一列,要得到与原始a相同的矩阵,需要在reshape中修改矩阵的大小。

%代码如下。

a=[ 1,1; 二、二; 三、三; 四、四; 五、三; 五、三; 五六; 七、四; 八、四; 八五; 九、四);

[C,ia,IC]=unique(a );

b=c(IC );

a=reshape(b,size ) a,1 ),size ) a,2 );

% 4.[C,ia,IC]=unique(a,' rows ' ) ) )。

also returns index vectors ia and ic,suchthatc=a(ia, ) and A=

C (集成电路, )。

a=[ 1,1; 二、二; 三、三; 四、四; 五、四; 五、三; 五六; 七、四; 八、四; 八五; 九、四);

([C,ia,IC]=unique(a,' rows ' ) ) )。

c=a(ia, ) ) ) )。

a=c(IC, ) ) ) ) )。

%

这里进行整行的比较,说明同上

% [C,ia,IC]=unique(a,setOrder ) and

[C,ia,IC]=unique(a,' rows ',setOrder ) return C in a specific

骑士。

% setOrder='sorted' returns the

values(orrows ) of C in sorted order。

% setOrder='stable' returns the

values(orrows ) of C in the same order as A .

% If no value is specified,the

默认设置。

%其中setOrder有两种格式。 sorted和

stable,sorted按照从小到大的顺序排序,是默认的形式,stable不改变原来矩阵的顺序

%代码如下

a=[ 5,3; 五、三; 五六; 1,1; 二、二; 三、三; 四、四; 七、四; 八、四; 八五; 九、四);

([C,ia,IC]=unique(a,' sorted ' ) ) ) ) ) ) ) ) ) ) ) c,ia,IC ) ) ) ) ) ) ) ) ) ) ) ) ) )

c=unique(a,' sorted ' ) )。

([C,ia,IC]=unique(a,' stable ' ) ) )。

c=unique(a,' stable ' ) )。

[C,ia,ic]=

unique(a,' rows ',' sorted ' ) )。

c=unique(a,' sorted ',' rows ' ) )。

[C,ia,ic]=

unique(a,' stable ',' rows ' ) )。

c=unique(a,' stable ',' rows ' ) )。

% [C,ia,IC]=unique(a,' legacy ),

[C,ia,IC]=unique(a,' rows ',' legacy ' ),

% [C,ia,ic]=

unique(a,occurrence,' legacy ' ),and [C,ia,ic]=

unique(a,' rows ',occurrence,' legacy ' ) )。

% legacy有两种格式:“第一”和“最后”

%

在此,主要对ia来说,对于重复值,如果是first则返回最初的坐标,如果是last则返回最后的坐标

%代码如下。

a=[ 5,3; 五、三; 五六; 1,1; 二、二; 三、三; 四、四; 七、四; 八、四; 八五; 九、四);

[C,ia,ic]=

unique(a,' rows ',' first ' ) )。

[C,ia,ic]=

unique(a,' rows ',' last ' ) )。

%这里相对于5、3,最先返回的是1,第二个返回的是2;

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。