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

2019-02-19 04:38发布

Using this method on a 8x8 matrix:

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

Outputs the format in 64x1 format (if no repeats are found) or nx1 if some repeats are found.

My question is how do I keep the matrix format without the sorting?

i need it to check unique(rows) for duplicates not unique cells. And to delete the duplicate rows but keep the format (dont arrange/sort).

1条回答
霸刀☆藐视天下
2楼-- · 2019-02-19 04:58

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
查看更多
登录 后发表回答