开发者

How can I tell if a rectangular matrix has duplicate rows in MATLAB?

I have an n-by-m rectang开发者_运维知识库ular matrix (n != m). What's the best way to find out if there are any duplicate rows in it in MATLAB? What's the best way to find the indices of the duplicates?


Use unique() to find the distinct row values. If you end up with fewer rows, there are duplicates. It'll also give you indexes of one location of each of the distinct values. All the other row indexes are your duplicates.

x = [
    1 1
    2 2
    3 3
    4 4
    2 2
    3 3
    3 3
    ];
[u,I,J] = unique(x, 'rows', 'first')
hasDuplicates = size(u,1) < size(x,1)
ixDupRows = setdiff(1:size(x,1), I)
dupRowValues = x(ixDupRows,:)


You can use the functions UNIQUE and SETDIFF to accomplish this:

>> mat = [1 2 3; 4 5 6; 7 8 9; 7 8 9; 1 2 3];    %# Sample matrix
>> [newmat,index] = unique(mat,'rows','first');  %# Finds indices of unique rows
>> repeatedIndex = setdiff(1:size(mat,1),index)  %# Finds indices of repeats

repeatedIndex =

     4     5


Run through the rows of the matrix, and for each pair, test if

row1 == row2


Say your matrix is M:

[S,idx1] = sortrows(M);
idx2 = find(all(diff(S,1) == 0,2));
out = unique(idx1([idx2;idx2+1]));

out will contain the duplicate row indices if any.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜