问题
I have this 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'
I need to delete the repeated rows but without changing the order of the whole rows... Note that I used code to find the unique rows but it changes the order of the rows:
[~,idx]=unique(cell2mat(levelx),'rows');
unique_levelx = levelx(idx,:);
回答1:
Try
unique(levelx,'rows','stable')
Note that in this statement I've assumed that levelx
is an array because that's how I set up your test data.
回答2:
I have a hack!
for li=1:size(levelx,1)
fn = regexprep( [levelx{li,:}], ':', '' );
s.(fn) = 1;
end
levelx = {};
fn = fieldnames( s );
for li=1:numel(fn)
t = regexp( rexexprep( fn{li}, 'GO', 'GO:' ), '(GO:\d+)', 'tokens' );
levelx(li, 1:3 ) = cellfun( @(x) x{1}, t, 'uni', false );
end
来源:https://stackoverflow.com/questions/13911689/how-to-delete-repeated-rows-with-out-re-ordering-them-in-matlab