How can I find indices of each row of a matrix which has a duplicate in matlab?

拈花ヽ惹草 提交于 2019-12-04 04:06:57

How about:

[~, ia, ic] = unique(A, 'rows')

setdiff(1:size(A,1), ia( sum(bsxfun(@eq,ic,(1:max(ic))))<=1 ))

Three other possibilities:

  1. Sort rows of the matrix (with sortrows), detect equal rows (with diff) and use indexing to undo the sorting:

    [As inds] = sortrows(A);
    ind = find(all(diff(As)==0,2));
    result = inds(union(ind,ind+1));
    
  2. Directly compare every row against every other row (with bsxfun):

    match = squeeze(all((bsxfun(@eq, A, permute(A, [3 2 1]))), 2));
    result = find(any(match - eye(size(A,1))));
    
  3. Use pdist with Hamming distance instead of bsxfun:

    match = ~squareform(pdist(A,'hamming'));
    result = find(any(match - eye(size(A,1))));
    

The advantage of approaches 2 and 3 is that you additionally get a (symmetric) matrix, match, which tells you which row equals which other. For your example,

    >> match
    match =
      1     1     0     1     0
      1     1     0     1     0
      0     0     1     0     0
      1     1     0     1     0
      0     0     0     0     1

One way to identify duplicates is to apply accumarray on the ic vector from unique. Then, setdiff will return the full list if indexes of duplicate rows.

[~, ia, ic] = unique(A,'rows')
dupRows = setdiff(1:size(A,1),ia(accumarray(ic,1)<=1))

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