how to order a matrix?

1 次查看(过去 30 天)
hi,
Say, I have a matrix,
A B C D
-6760,691 0 -1 0
-3380,495 1 -1 1
-6760,691 0 -1 0
-3380,395 -1 0 -1
-3380,395 -1 0 -1
-6760,691 0 -1 0
-3380,495 -1 -1 1
-6760,691 0 -1 0
-3380,395 -1 0 -1
3380,195 1 0 1
And I want to get,
A B C D
-6760,691 0 -1 0
-3380,495 1 -1 1
-1 -1 1
-3380.395 -1 0 -1
3380,195 1 0 1
Here, I need to order the unique values of A which is done on the 1st column. I'll have to write the different patterns of B, C, D on the basis of same value of A. And, if there is any more repeated values of B,C,D for the same value of A, i'll have to keep only 1 pattern (row).
Can anyone please help me?

采纳的回答

Andrei Bobrov
Andrei Bobrov 2018-11-30
编辑:Andrei Bobrov 2018-12-1
EDIT
>> B = [-3380.5 1 -1 1
-6760.7 0 -1 0
-3380.4 -1 0 -1
-3380.4 -1 0 -1
-6760.7 0 -1 0
-3380.5 -1 -1 1
-6760.7 0 -1 0
-3380.4 -1 0 -1
3380.2 1 0 1];
>> A = unique(B,'rows')
A =
-6760.7 0 -1 0
-3380.5 -1 -1 1
-3380.5 1 -1 1
-3380.4 -1 0 -1
3380.2 1 0 1
>> A([false;diff(A(:,1)) == 0],1) = nan
A =
-6760.7 0 -1 0
-3380.5 -1 -1 1
NaN 1 -1 1
-3380.4 -1 0 -1
3380.2 1 0 1
>>
  4 个评论
Andrei Bobrov
Andrei Bobrov 2018-12-3
A = [ -3380.5 1 -1 1 0 0 0 0
-6760.7 0 -1 0 0 1 0 0
-3380.4 -1 0 -1 0 1 1 0
-3380.4 -1 0 -1 1 0 0 0
-6760.7 0 -1 0 0 0 1 0
-3380.5 -1 -1 1 1 0 1 0
-6760.7 0 -1 0 1 0 1 1
-3380.4 -1 0 -1 0 0 0 1
3380.2 1 0 1 0 1 0 1];
[~,b] = unique(A(:,1:4),'rows');
B = A(b,:);
B([false;diff(B(:,1))==0],1) = nan;
Sky Scrapper
Sky Scrapper 2018-12-3
it's working properly. Thanks!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by