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

前端 未结 4 784
南笙
南笙 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 12:55

    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,:)
    

提交回复
热议问题