how to find unique rows in cell array in matlab

201 views (last 30 days)
hi.. how to find the unique rows for the following cell array:
levelx=
'GO:0016787' 'GO:0006412' 'GO:0030533'
'GO:0008150' 'GO:0006412' 'GO:0030533'
'GO:0006810' 'GO:0006412' 'GO:0030533'
'GO:0016787' 'GO:0006412' 'GO:0030533'
'GO:0008150' 'GO:0006412' 'GO:0030533'
'GO:0006810' 'GO:0006412' 'GO:0030533'
'GO:0016787' 'GO:0006412' 'GO:0030533'
'GO:0008150' 'GO:0006412' 'GO:0030533'
'GO:0006810' 'GO:0006412' 'GO:0030533'
'GO:0016787' 'GO:0006412' 'GO:0030533'
'GO:0008150' 'GO:0006412' 'GO:0030533'
'GO:0006810' 'GO:0006412' 'GO:0030533'
'GO:0016787' 'GO:0006412' 'GO:0030533'
'GO:0008150' 'GO:0006412' 'GO:0030533'
'GO:0006810' 'GO:0006412' 'GO:0030533'
'GO:0016787' 'GO:0006412' 'GO:0030533'
'GO:0008150' 'GO:0006412' 'GO:0030533'
'GO:0006810' 'GO:0006412' 'GO:0030533'
'GO:0016787' 'GO:0006412' 'GO:0030533'
'GO:0008150' 'GO:0006412' 'GO:0030533'
'GO:0006810' 'GO:0006412' 'GO:0030533'
'GO:0016787' 'GO:0006412' 'GO:0030533'
'GO:0008150' 'GO:0006412' 'GO:0030533'
'GO:0006810' 'GO:0006412' 'GO:0030533'
'GO:0016787' 'GO:0006412' 'GO:0030533'
'GO:0008150' 'GO:0006412' 'GO:0030533'
'GO:0006810' 'GO:0006412' 'GO:0030533'
'GO:0016787' 'GO:0006412' 'GO:0030533'
'GO:0008150' 'GO:0006412' 'GO:0030533'
'GO:0006810' 'GO:0006412' 'GO:0030533'

Accepted Answer

Matt J
Matt J on 4 Nov 2012
unique(cell2mat(levelx),'rows')
  2 Comments
Jwana
Jwana on 4 Nov 2012
thank you for your response .. but how can I return them again to cell ?? because it shows like this:
GO:0006810GO:0005215GO:0030533 GO:0006810GO:0006412GO:0030533 GO:0008150GO:0005215GO:0030533 GO:0008150GO:0006412GO:0030533 GO:0008150GO:0006810GO:0003774 GO:0008150GO:0006810GO:0005215 GO:0008150GO:0006810GO:0006412 GO:0008150GO:0006810GO:0030533 GO:0008150GO:0016787GO:0005215 GO:0008150GO:0016787GO:0006412 GO:0016787GO:0005215GO:0030533 GO:0016787GO:0006412GO:0030533
Matt J
Matt J on 4 Nov 2012
Edited: Matt J on 4 Nov 2012
[~,idx]=unique(cell2mat(levelx),'rows');
unique_levelx = levelx(idx,:);

Sign in to comment.

More Answers (2)

Jim Hokanson
Jim Hokanson on 1 May 2013
The solution above works since the contents concatenate into a hyperrectangle. Put more plainly, in this case, the solution works since all the entries are the same size.
A more general solution can be found at my FEX submission: 25917: Unique Rows for a Cell Array

Andreas Thealnder Bertelsen
For unstructured data use table.
pairs{1,1} = '02-Target-180814_1800.wav';
pairs{1,2} = 1525;
pairs{2,1} = '02-Target-180814_1800.wav';
pairs{2,2} = 1563;
pairs{3,1} = '02-Target-180814_1800.wav';
pairs{3,2} = 1525;
table2cell(unique(cell2table(pairs)))

Categories

Find more on Operating on Diagonal Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!