如何计算一些随机生成的矩阵和matlab中给定数组之间的距离

How to calculate distance between some randomly generated matrix and a given array in matlab

在此代码中,生成了 50 个随机坐标(人口规模为 50)。 我有一个数组,

B = [150 90; -100 -120; -80 130; 140 -70; 60 120; -90 -130].

我想计算每50个坐标与B数组每个坐标的距离。计算距离后,我必须将所有距离值分别保存在数组(或矩阵)中,以便之后检索它们。 请帮我计算一下距离。

clear all
clc

%Common Parameter Setting
N=2;        % Number of variables
M=50;       % Populations size 50
F=0.5;      % Mutation factor
C=0.9;      % Crossover rate
I_max=20;   % Max iteration time
Run=1;      % The number of test time
X_max=[100,100];
X_min=[-100,-100];

%Func=@Rastrigin;

% 2.The whole test loop
for r=1:Run
    iter=0;
    % 1.Generate MxN matrix
    for m=1:M
        for n=1:N
            X(m,n)=X_min(n)+rand()*(X_max(n)-X_min(n));
        end
        fprintf('value of X:');
        disp(X);
    end
end

最后一部分没完全看懂。但是如果你有 2 个坐标列表,比如

x = randn(10,2); %10 points in 2D
y = randn(3,2); % 3 others points in 2D

并且您想要 x 中所有点与 y 中所有点之间的成对距离,您可以使用 pdist2

D = pdist2(x,y);

现在 D(1,2) 将是从 x(1)y(2) 的欧氏距离,依此类推。