junk, index and unique on a matrix (how to keep matrix format)

后端 未结 1 357
醉梦人生
醉梦人生 2021-01-14 06:02

Using this method on a 8x8 matrix:

>> [junk,index] = unique(data,\'first\');        %# Capture the index, ignore junk
>> data(sort(index))                


        
相关标签:
1条回答
  • 2021-01-14 06:49

    If you want unique rows, while keeping original order, try this:

    [M,ind] = unique(data, 'rows', 'first');
    [~,ind] = sort(ind);
    M = M(ind,:);
    

    Example:

    >> data = randi(2,[8 3]);
    data =
         1     2     1
         1     2     1
         1     1     2
         2     2     2
         1     1     1
         2     2     2
         2     2     2
         2     1     1
    >> M
    M =
         1     2     1
         1     1     2
         2     2     2
         1     1     1
         2     1     1
    
    0 讨论(0)
提交回复
热议问题