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

前端 未结 4 791
南笙
南笙 2020-12-20 12:48

I have an n-by-m rectangular 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 t

4条回答
  •  余生分开走
    2020-12-20 13:18

    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
    

提交回复
热议问题