计算两个矩阵中行的每个组合之间的距离 [英] Compute distance between each combination of rows in two matrices

查看:352
本文介绍了计算两个矩阵中行的每个组合之间的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Matlab中遇到以下问题:

I've got the following problem in Matlab:

让我们假设我们有两个大小相同的矩阵 A B ,其中每一行( m )代表了一段时间内的数据集( n ).矩阵 A 包含参考数据,矩阵 B 包含要测试的数据.现在,我想使用

Let's assume we have two matrices A and B with the same size, where each row (m) represents a dataset over time (n). Matrix A contains the reference data and Matrix B the data to be tested. I now want to compute the relative distance between each and every combination of rows in A and B using

d(m_i,m_j) = sqrt(sum((A(m_x,:)-B(m_y,:).^2))

通过for循环解决该问题会导致

Solving this via a for loop would result in

for m_x = 1:size(A,2)
    for m_y = 1:size(A,2)
        d(m_i,m_j) = sqrt(sum((A(m_i,:)-B(m_j,:).^2));
    end
end

是否有更优雅(甚至更快)的方式?

Is there a more elegant (and maybe faster) way of doing so?

推荐答案

是的.您可以使用pdist2(请参见 doc ):

Yes, there is. You can use pdist2 (see doc):

d = pdist2(A,B);

条目d(m,n)A(m,:)B(n,:)之间的距离.

这篇关于计算两个矩阵中行的每个组合之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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