如何判断矩形矩阵在MATLAB中是否有重复的行? [英] How can I tell if a rectangular matrix has duplicate rows in MATLAB?

查看:1097
本文介绍了如何判断矩形矩阵在MATLAB中是否有重复的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个n×m矩形矩阵(n!= m)。找到MATLAB中是否有重复行的最佳方式是什么?找到重复的索引的最佳方式是什么?

解决方案

使用unique()来查找不同的行值。如果最后的行数较少,则有重复的。它还会给出每个不同值的一个位置的索引。所有其他行索引是您的重复项。

  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)尺寸(X,1)
ixDupRows = setdiff(1:尺寸(X,1),I)
dupRowValues = X(ixDupRows,:)


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

这篇关于如何判断矩形矩阵在MATLAB中是否有重复的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆