Remove Duplicate Rows in a Cell Array

Hello,
I'm trying to remove duplicate rows in a cell array. I'm familiar with the 'unique' function for use on vectors (or individual columns of a cell array) but I'm stumped on how to work on the whole array at once.
What I'm looking for is the same as the Excel button "Remove Duplicates" which does this:
withduplicates={
'Bear' 'Chicago' '1 yr'
'Bear' 'Chicago' '1 yr'
'Bear' 'Chicago' '1 yr'
'Bear' 'Miami' '2 yr'
'Bear' 'Miami' '2 yr'
'Lion' 'Denver' '1 yr'
'Lion' 'Miami' '2 yr'
'Lion' 'Miami' '2 yr'
'Lion' 'Miami' '2 yr'
'Tiger' 'Denver' '1 yr'
'Tiger' 'Chicago' '1 yr'
'Tiger' 'Chicago' '1 yr'}
withoutduplicates={
'Bear' 'Chicago' '1 yr'
'Bear' 'Miami' '2 yr'
'Lion' 'Denver' '1 yr'
'Lion' 'Miami' '2 yr'
'Tiger' 'Denver' '1 yr'
'Tiger' 'Chicago' '1 yr'}
Any help is greatly appreciated.

 採用された回答

Matt J
Matt J 2012 年 11 月 19 日

2 投票

wd=withduplicates;
[~,idx]=unique( strcat(wd(:,1),wd(:,2),wd(:,3)) , 'rows');
withoutduplicates=wd(idx,:)

6 件のコメント

Nicholas
Nicholas 2012 年 11 月 19 日
That worked! Thank you.
Nicholas
Nicholas 2012 年 11 月 19 日
I'm getting an orange warning in the Command Window every time I run the code you gave:
"Warning: The 'rows' input is not supported for cell array inputs."
How do I suppress this?
Thanks.
Matt J
Matt J 2012 年 11 月 19 日
編集済み: Matt J 2012 年 11 月 19 日
Ah. Just omit the 'rows' input argument, then. It's not necessary.
Arne T
Arne T 2020 年 9 月 4 日
How do you handle following Problem:
withduplicates={
'exampleA' 'example'
'example' 'Aexample'}
Cause this rows are not the same, but your code would delete one of them.
Matt J
Matt J 2020 年 9 月 4 日
Nowadays, you would just use strings,
withoutduplicates=unique(string(withduplicates),'rows')
Michael Boateng
Michael Boateng 2021 年 2 月 16 日
This worked well. Thank you.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCell Arrays についてさらに検索

質問済み:

2012 年 11 月 19 日

コメント済み:

2021 年 2 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by