How to remove repeating pair

2 ビュー (過去 30 日間)
Mekala balaji
Mekala balaji 2016 年 7 月 19 日
回答済み: Bhagyesh Shiyani 2020 年 4 月 28 日
Hi,
I have the below table,(my 3rd column is just a Rsquare value)
A B 1
N M 1
Y L 1
B A 1
K U 1
L Y 1
A B 1
U K 1
P G 1
M N 1
X Z 1
U K 1
I only want to keep one combination out of the below: in row 1, row4, row7 (in my definition, A-->B is same B--> A, and also remove repeating pair like row1 and row3 is just a duplicate).Filtering is just based on column1 & column2.
A B 1
B A 1
A B 1
From above only keep
A B 1
and finally my output should be as below:
A B 1
N M 1
Y L 1
K U 1
P G 1
X Z 1
Many thanks in advance,

採用された回答

Andrei Bobrov
Andrei Bobrov 2016 年 7 月 23 日
編集済み: Andrei Bobrov 2016 年 7 月 23 日
without 'stable'
A={'HT0MEK1' 'H0MEK1'
'HT0MEK2' '78 T0MEK2'
'JT0KEL3' 'JKEL3_ul'
'JT0KEL4' '12 0KEL4_45'
'T0KEL5' 'P0KEL5_89K_45um'
'J0KEL6' 'KEL6_Y_9Et4_y'
'BK0JIL7' 'LG_JIL7'
'HA0GEK8' 'K0GEK8'
'H0GEK9' 'H0GEK9'
'HT0MEK1' 'H0MEK1'
'HT0MEK2' '78 T0MEK2'
'JKEL3_ul' 'JT0KEL3'
'12 0KEL4_45' 'JT0KEL4'
'KEL6_Y_9Et4_y' 'J0KEL6'
'12 0KEL4_45' 'JT0KEL4'
'P0KEL5_89K_45um' 'T0KEL5'
'KEL6_Y_9Et4_y' 'J0KEL6'};
a1 = sort(A')';
[~,b0,c0] = unique(a1,'first');
[~,ii] = sort(b0);
[~,i2] = sort(ii);
c = reshape(i2(c0),size(A));
[~,b] = unique(c,'first','rows');
[~,ii] = sort(b);
[~,i2] = sort(ii);
b1 = b(ii);
out = a1(b1,:);

その他の回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 7 月 19 日
編集済み: Azzi Abdelmalek 2016 年 7 月 19 日
A={'A' 'B' 1
'N' 'M' 1
'Y' 'L' 1
'B' 'A' 1
'K' 'U' 1
'L' 'Y' 1
'A' 'B' 1
'U' 'K' 1
'P' 'G' 1
'M' 'N' 1
'X' 'Z' 1
'U' 'K' 1}
b=arrayfun(@(x) sort(strjoin(A(x,1:2),'')),(1:size(A,1))','un',0)
[~,ii]=unique(b,'stable')
out=A(ii,:)
Result
out =
'A' 'B' [1]
'N' 'M' [1]
'Y' 'L' [1]
'K' 'U' [1]
'P' 'G' [1]
'X' 'Z' [1]
  5 件のコメント
Guillaume
Guillaume 2016 年 7 月 20 日
編集済み: Guillaume 2016 年 7 月 20 日
As I said, it's always a good idea to mention the version of matlab you're using if it's not current.
How old is your version of matlab, if the 'stable' option of unique is not supported? Indeed, the purpose of 'stable' is to preserve the order in which the elements first appear. Since your version does not support that option, you'll have to go with the default, sorted output. The order of your output should not matter anyway.
If it does matter to you, you will either have to upgrade to a newer version of matlab or write your own stable sort.
Mekala balaji
Mekala balaji 2016 年 7 月 23 日
編集済み: Azzi Abdelmalek 2016 年 7 月 23 日
Sir, I upgrade, stable is working, but if I use my input cell array as below:
A={'HT0MEK1' 'H0MEK1'
'HT0MEK2' '78 T0MEK2'
'JT0KEL3' 'JKEL3_ul'
'JT0KEL4' '12 0KEL4_45'
'T0KEL5' 'P0KEL5_89K_45um'
'J0KEL6' 'KEL6_Y_9Et4_y'
'BK0JIL7' 'LG_JIL7'
'HA0GEK8' 'K0GEK8'
'H0GEK9' 'H0GEK9'
'HT0MEK1' 'H0MEK1'
'HT0MEK2' '78 T0MEK2'
'JKEL3_ul' 'JT0KEL3'
'12 0KEL4_45' 'JT0KEL4'
'KEL6_Y_9Et4_y' 'J0KEL6'
'12 0KEL4_45' 'JT0KEL4'
'P0KEL5_89K_45um' 'T0KEL5'
'KEL6_Y_9Et4_y' 'J0KEL6'}
b=arrayfun(@(x) sort([A{x, [1 2]}]),(1:size(A,1))','un',0);
Then I get my b As:
b =
'0011EEHHKKMMT'
' 002278EEHKKMMTT'
'033EEJJKKLLT_lu'
' 00124445EEJKKLLT_'
'00455589EEKKKLLPT__mu'
'04669EEEJKKLLY___ty'
'077BGIIJJKLLL_'
'0088AEEGGHKKK'
'0099EEGGHHKK'
'0011EEHHKKMMT'
' 002278EEHKKMMTT'
'033EEJJKKLLT_lu'
' 00124445EEJKKLLT_'
'04669EEEJKKLLY___ty'
' 00124445EEJKKLLT_'
'00455589EEKKKLLPT__mu'
'04669EEEJKKLLY___ty'
How to get correct array;

サインインしてコメントする。


Bhagyesh Shiyani
Bhagyesh Shiyani 2020 年 4 月 28 日
how to do it for numbers. same like strings

カテゴリ

Help Center および File ExchangeDownloads についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by