フィルターのクリア

How to filter duplicate entries

2 ビュー (過去 30 日間)
Mekala balaji
Mekala balaji 2018 年 8 月 8 日
コメント済み: Mekala balaji 2018 年 8 月 9 日
Hi,
I have below entries (cell array matrix),
FirstEntry SecondEntry Index Score
A B 5 25
C D 62 8
C K 31 2
B A 6 25
K C 9 2
The entry A & B (in row 1)is same as B & A(in row 4) (without consider its first entry or second entry), I only want to keep one entry for this kind of entries. Likewise, row 3 (c & K) is same as row 5 (K & C)
My desired output:
FirstEntry SecondEntry Index Score
A B 5 25
C D 62 8
C K 31 2
Kindly some one help, many thanks in advance,

採用された回答

Adam Danz
Adam Danz 2018 年 8 月 8 日
編集済み: Adam Danz 2018 年 8 月 8 日
Your original cell array is named 't'.
t = {
'A' 'B' 5 25
'C' 'D' 62 8
'C' 'K' 31 2
'B' 'A' 6 25
'K' 'C' 9 2};
'unqIdx' is an index of row numbers of 't' that are unique.
'desired' is your unique array without duplicates.
[~, unqIdx] = unique(cellfun(@strcat, sort(t(:,[1,2])')'), 'rows');
desired = t(unqIdx, :);
  5 件のコメント
Adam Danz
Adam Danz 2018 年 8 月 9 日
I see. Here's the updated code that works with the old and new dataset. I could no longer do it in 1 line of code. The new version uses 2 lines and the use of cellfun() is no longer necessary. I also added the 'stable' parameter to unique() so that your final unique array is in the same order as your input array.
tSort = sort(t(:,[1,2])')';
[~, unqIdx] = unique(strcat(tSort(:,1), tSort(:,2)), 'rows', 'stable')
t(unqIdx, :)
Mekala balaji
Mekala balaji 2018 年 8 月 9 日
Thanks Sir,
It works.

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by