Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Remove Duplicate Rows in a Cell Array

Asked by Nicholas on 19 Nov 2012

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.

0 Comments

Nicholas

Products

No products are associated with this question.

1 Answer

Answer by Matt J on 19 Nov 2012
Accepted answer
wd=withduplicates;
[~,idx]=unique(  strcat(wd(:,1),wd(:,2),wd(:,3)) , 'rows');
withoutduplicates=wd(idx,:)

3 Comments

Nicholas on 19 Nov 2012

That worked! Thank you.

Nicholas on 19 Nov 2012

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 on 19 Nov 2012

Ah. Just omit the 'rows' input argument, then. It's not necessary.

Matt J

Contact us