How to remove repeating pair
이전 댓글 표시
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,
채택된 답변
추가 답변 (2개)
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
Mekala balaji
2016년 7월 20일
Guillaume
2016년 7월 20일
You're using a version older than 2013a, where strjoin was introduced. It's always a good idea to mention it your question if you're using a very old version.
In this case,
b=arrayfun(@(x) sort([A{x, [1 2]}]),(1:size(A,1))','un',0)
will work just as well.
Mekala balaji
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
2016년 7월 23일
편집: Azzi Abdelmalek
2016년 7월 23일
Bhagyesh Shiyani
2020년 4월 28일
0 개 추천
how to do it for numbers. same like strings
카테고리
도움말 센터 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!