How to delete repeated rows with out re-ordering them in matlab

一个人想着一个人 提交于 2019-12-10 17:16:54

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!