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

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

问题描述

我有一个 n×m 矩形矩阵 (n != m).在 MATLAB 中找出是否有任何重复行的最佳方法是什么?查找重复项索引的最佳方法是什么?

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?

推荐答案

使用 unique() 查找不同的行值.如果您最终得到的行数较少,则存在重复项.它还将为您提供每个不同值的一个位置的索引.所有其他行索引都是您的重复项.

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天全站免登陆