在 MATLAB 中使用范数函数

Using norm Function In MATLAB

我有一个数据矩阵,它是一些点的坐标和 5 个簇的坐标

data = [randi(100,100,1),randi(100,100,1)];
x_Clusters = [20 5 12 88 61];
y_Clusters = [10 50 14 41 10];
Coordinates_Of_Clusters = [x_Clusters',y_Clusters']; 

我想使用范数函数来确定上述坐标的5个已知簇的中心到我的数据的距离。我该怎么做?

功能norm(X)norm(X,2)相同。 Matlab默认使用2范数(欧式距离)。

使用您的代码开始:

% number of data points (trying to harcode less values)
n_points = 100;

data = [randi(n_points,n_points,1),randi(n_points,n_points,1)];
x_Clusters = [20 5 12 88 61];
y_Clusters = [10 50 14 41 10];
Coordinates_Of_Clusters = [x_Clusters',y_Clusters']; 

% number of clusters 
n_clusters = size(Coordinates_Of_Clusters,1);

% option 1: output is 100-by-10
output_matrix_100x10 = zeros(n_points,2*n_clusters);

% option 2: output is 500-by-2
output_matrix_500x2  = zeros(n_points*n_clusters,2);

然后对所有簇 (n_clusters) 和每个点 (n_points) 使用 for 循环:

for n = 1:n_clusters
    for i = 1:n_points

        % option 1
        output_matrix_100x10(i,(n-1)*2+1:(n-1)*2+2) = ...
            norm(data(i,:)-Coordinates_Of_Clusters(n,:), 2);

        % option 2
        output_matrix_500x2((n-1)*n_points+i,1:2) = ...
            norm(data(i,:)-Coordinates_Of_Clusters(n,:), 2);

    end
end