MATLAB矢量化的成对距离

发布于 2025-01-22 12:13:57 字数 732 浏览 0 评论 0原文

我正在努力化一个函数,该函数在两个矢量x = 2xn和v = 2xm之间执行一些成对的差异,对于某个任意n,M。我在n = 1时可以工作功能适用于具有n个任意的输入。

实际上,我希望此功能要做的是x的每一列找到x(:,列)(A 2x1)和V(A 2xm)之间的范围差异。

类似的帖子是 this ,尽管我无法推广它。

当前实施

function mat = vecDiff(x,v)

diffVec = bsxfun(@minus, x, v);
mat = diffVec ./ vecnorm(diffVec);

示例

x =
     1
     1

v = 
     1     3     5
     2     4     6

----
vecDiff(x,v) =
         0   -0.5547   -0.6247
   -1.0000   -0.8321   -0.7809

I'm struggling to vectorise a function which performs a somewhat pairwise difference between two vectors x = 2xN and v = 2xM, for some arbitrary N, M. I have this to work when N = 1, although, I would like to vectorise this function to apply to inputs with N arbitrary.

Indeed, what I want this function to do is for each column of x find the normed difference between x(:,column) (a 2x1) and v (a 2xM).

A similar post is this, although I haven't been able to generalise it.

Current implementation

function mat = vecDiff(x,v)

diffVec = bsxfun(@minus, x, v);
mat = diffVec ./ vecnorm(diffVec);

Example

x =
     1
     1

v = 
     1     3     5
     2     4     6

----
vecDiff(x,v) =
         0   -0.5547   -0.6247
   -1.0000   -0.8321   -0.7809

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仙女山的月亮 2025-01-29 12:13:57

您的方法可以如下适合您的需求:

  1. permute xv的尺寸,以便其列数变为 third>第三 dimension。我在下面的代码中选择v
  2. 这让您可以利用(或等效地 bsxfun)to计算A 2×M×n差异数组,其中mn nxv的列数。
  3. 计算 vector-wise(2-)norm 沿着第一个维度并再次使用隐式扩展来标准化此数组:
x = [1 4 2 -1; 1 5 3 -2];
v = [1 3 5; 2 4 6];
diffVec = x - permute(v, [1 3 2]);
diffVec = diffVec./vecnorm(diffVec, 2, 1);

如果您希望按其他顺序输出尺寸,则可能需要以不同的方式应用PERTER

Your approach can be adapted as follows to suit your needs:

  1. Permute the dimensions of either x or v so that its number of columns becomes the third dimension. I'm choosing v in the code below.
  2. This lets you exploit implicit expansion (or equivalently bsxfun) to compute a 2×M×N array of differences, where M and N are the numbers of columns of x and v.
  3. Compute the vector-wise (2-)norm along the first dimension and use implicit expansion again to normalize this array:
x = [1 4 2 -1; 1 5 3 -2];
v = [1 3 5; 2 4 6];
diffVec = x - permute(v, [1 3 2]);
diffVec = diffVec./vecnorm(diffVec, 2, 1);

You may need to apply permute differently if you want the dimensions of the output in another order.

眼藏柔 2025-01-29 12:13:57

假设您的两个输入矩阵是A(A 2 X N矩阵)和B(A 2 X M矩阵),其中每列代表不同的观察值(请注意,这不是表示数据的传统方式)。

请注意,输出的大小为n x m x 2。
out = zeros(n,m,2);

我们可以使用内置函数pdist2找到它们之间的距离。

dists = pdist2(a。',b。');(矩阵的方向所需的换位)

才能获得单个x和y距离,我想到的最简单的方法就是使用repmat:

xdists = repmat(A(1,:).', 1, M) - repmat(B(1,:), N, 1);
ydists = repmat(A(2,:).', 1, M) - repmat(B(2,:), N, 1);

然后,我们可以通过前面找到的距离进行归一化:

out(:,:,1) = xdists./dists; 
out(:,:,2) = ydists./dists;

这返回矩阵out,其中位置的元素(i,j,:)a(:,i)b(:j)之间的规范距离。

Suppose your two input matrices are A (a 2 x N matrix) and B (a 2 x M matrix), where each column represents a different observation (note that this is not the traditional way to represent data).

Note that the output will be of the size N x M x 2.
out = zeros(N, M, 2);

We can find the distance between them using the builtin function pdist2.

dists = pdist2(A.', B.'); (with the transpositions required for the orientation of the matrices)

To get the individual x and y distances, the easiest way I can think of is using repmat:

xdists = repmat(A(1,:).', 1, M) - repmat(B(1,:), N, 1);
ydists = repmat(A(2,:).', 1, M) - repmat(B(2,:), N, 1);

And we can then normalise this by the distances found earlier:

out(:,:,1) = xdists./dists; 
out(:,:,2) = ydists./dists;

This returns a matrix out where the elements at position (i, j, :) are the components of the normed distance between A(:,i) and B(:,j).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文