如何在matlab中找到具有重复的矩阵的每一行的索引? [英] How can I find indices of each row of a matrix which has a duplicate in matlab?
本文介绍了如何在matlab中找到具有重复的矩阵的每一行的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想找到具有重复的矩阵的所有行的索引。例如
I want to find the indices all the rows of a matrix which have duplicates. For example
A = [1 2 3 4
1 2 3 4
2 3 4 5
1 2 3 4
6 5 4 3]
要返回的向量将是 [1,2,4]
很多类似的问题建议使用唯一的
函数,我尝试过但最接近我想要的是:
A lot of similar questions suggest using the unique
function, which I've tried but the closest I can get to what I want is:
[C, ia, ic] = unique(A, 'rows')
ia = [1 3 5]
m = 5;
setdiff(1:m,ia) = [2,4]
但是使用 unique
我只能提取一行的第二,第三,第四...等实例,我还需要获得第一个。有什么办法可以做到吗?
But using unique
I can only extract the 2nd,3rd,4th...etc instance of a row, and I need to also obtain the first. Is there any way I can do this?
NB:它必须是一个不涉及循环遍历行的方法,因为我在处理大型稀疏矩阵。
NB: It must be a method which doesn't involve looping through the rows, as I'm dealing with large sparse matrices.
推荐答案
如何:
[~, ia, ic] = unique(A, 'rows')
setdiff(1:size(A,1), ia( sum(bsxfun(@eq,ic,(1:max(ic))))<=1 ))
这篇关于如何在matlab中找到具有重复的矩阵的每一行的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文