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

梦想与她 提交于 2019-12-19 09:45:30

问题


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:


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


来源:https://stackoverflow.com/questions/7781749/junk-index-and-unique-on-a-matrix-how-to-keep-matrix-format

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