gpt4 book ai didi

matlab - 跨两种模式的交叉成对距离测量

转载 作者:太空宇宙 更新时间:2023-11-03 20:16:00 25 4
gpt4 key购买 nike

我有一个问题。我正在尝试计算向量之间的成对距离。先解释一下问题:我有两组向量XY . X有三个向量 x1 , x2x3 . Y有三个向量 y1 , y2y3 .注意 X 中的向量和 Y长度为 mn分别。让数据集表示为这张图片:

enter image description here

我正在尝试计算这样的相似度矩阵:

enter image description here .现在解释了不同颜色编码的部分 - 所有那些标有 0 的单元格不需要计算。我故意把它写成 100 (它可以是任何值)。必须计算灰色单元格。相似性得分计算为 L2 (xi-xj) 的规范+ L2 (yi-yj) 的规范.

这意味着条目是

M((x_i,y_j), (x_k,y_l)) := norm(x_i-x_k,2) + norm(y_j-y_l,2)

我已经写了一个基本的代码来做到这一点:

clc;clear all;close all;
%% randomly generate data
m=3; n1=4; n2=6;
train_a_mean = rand(m,n1);
train_b_mean = rand(m,n2);
p = size(train_a_mean,1)*size(train_b_mean,1);
score_mean_ab = zeros(p,p);

%% This is to store the index variables
%% This is required for futu
idx1 = score_mean_ab;
idx2 = idx1; idx3 = idx1; idx4 = idx1;

a=1; b=1;
for i=1:size(score_mean_ab,1)
c = 1; d = 1;
for j=1:size(score_mean_ab,2)
if (a==c)
score_mean_ab(i,j) = 100;
else
%% computing distances between the different modalities and
%% summing them up
score_mean_ab(i,j) = norm(train_a_mean(a,:)-train_a_mean(c,:),2) ...
+ norm(train_b_mean(b,:)-train_b_mean(d,:),2);
end
%% saving the indices
idx1(i,j)=a; idx2(i,j)=b; idx3(i,j)=c; idx4(i,j)=d;
%% updating the values of c and d
if mod(d,size(train_a_mean,1))==0
c = c + 1;
d = 1;
else
d = d+1;
end
end
%% updating the values of a and b
if mod(b,size(train_a_mean,1))==0
a = a + 1;
b = 1;
else
b = b+1;
end
end

对于矩阵的干样本运行:我得到这些结果 -

score_mean_ab =

100.0000 100.0000 100.0000 0.6700 1.6548 1.5725 0.8154 1.8002 1.7179
100.0000 100.0000 100.0000 1.6548 0.6700 1.5000 1.8002 0.8154 1.6454
100.0000 100.0000 100.0000 1.5725 1.5000 0.6700 1.7179 1.6454 0.8154
0.6700 1.6548 1.5725 100.0000 100.0000 100.0000 1.3174 2.3022 2.2200
1.6548 0.6700 1.5000 100.0000 100.0000 100.0000 2.3022 1.3174 2.1475
1.5725 1.5000 0.6700 100.0000 100.0000 100.0000 2.2200 2.1475 1.3174
0.8154 1.8002 1.7179 1.3174 2.3022 2.2200 100.0000 100.0000 100.0000
1.8002 0.8154 1.6454 2.3022 1.3174 2.1475 100.0000 100.0000 100.0000
1.7179 1.6454 0.8154 2.2200 2.1475 1.3174 100.0000 100.0000 100.0000

但是我的代码很慢。我进行了很少的样本运行并得到了这些结果:

m=3; n1=3; n2=3;
Elapsed time is 0.000363 seconds.

m=10; n1=3; n2=3;
Elapsed time is 0.042015 seconds.

m=10; n1=1800; n2=1800;
Elapsed time is 0.230046 seconds.

m=20; n1=1800; n2=1800;
Elapsed time is 4.309134 seconds.

m=30; n1=1800; n2=1800;
Elapsed time is 23.058106 seconds.

我的问题:

  1. 通常我的值为 m~100n1~2000n2~2000 .我自己的代码在这一点上崩溃了。有什么优化的方法可以做到这一点吗?
  2. 内置的matlab函数可以吗pdist2 用于此目的?

注意:向量实际上是行向量的形式,n1的值和 n2可能不相等。

最佳答案

这是一种方法。这将计算所有条目。

m = 3;             %// number of (row) vectors in X and in Y
n1 = 3; %// length of vectors in X
n2 = 3; %// length of vectors in Y
X = rand(m, n1); %// random data: X
Y = rand(m, n2); %// random data: Y

[ii, jj] = ndgrid(1:m);
U = reshape(sqrt(sum((X(ii,:)-X(jj,:)).^2, 2)), m, m);
V = reshape(sqrt(sum((Y(ii,:)-Y(jj,:)).^2, 2)), m, m);
result = U(ceil(1/m:1/m:m), ceil(1/m:1/m:m)) + repmat(V, m, m);

或者您可以使用 bsxfun 而不是 ndgrid:

U = sqrt(sum(bsxfun(@minus, permute(X, [1 3 2]), permute(X, [3 1 2])).^2, 3));
V = sqrt(sum(bsxfun(@minus, permute(Y, [1 3 2]), permute(Y, [3 1 2])).^2, 3));
result = U(ceil(1/m:1/m:m), ceil(1/m:1/m:m)) + repmat(V, m, m);

关于matlab - 跨两种模式的交叉成对距离测量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28719829/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com