【问题标题】:Using norm Function In MATLAB在 MATLAB 中使用 norm 函数
【发布时间】:2021-01-13 03:29:22
【问题描述】:

我有一个数据矩阵,它是一些点的坐标和 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']; 

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

【问题讨论】:

  • 如果要计算 2 范数欧几里得距离,则需要使用 norm(X)。 Matlab 默认使用 2-范数。
  • 为什么要使用norm?它不是特别适合。
  • pdist2 在统计工具箱中执行此操作。

标签: matlab function cluster-analysis euclidean-distance norm


【解决方案1】:

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

【讨论】:

  • 感谢您的回答,但这不是我想要的。我想要我的数据和集群中心之间的距离,如我的问题中所示的 Coordinates_of_Clusters
  • 我现在明白了,我将在上面编辑我的回复。然后 2 范数将是 x1 和 x2 之间的距离,由 ((x1-x2)^2)^0.5 给出,也称为欧几里得距离。这也适用于更高的维度。
  • 您使用的是什么版本的 Matlab?变量是data 还是Data?当我复制它时,该代码对我有用。您是否还有其他变量可能会覆盖这些变量?
  • 在命令窗口中复制并过去这个“size([randi(100,100,1),randi(100,100,1)])',然后告诉我你在 R2013 中得到了什么。
  • 因此对于您的100-by2 矩阵,R2013 不想计算 2 范数,但您可以通过另一个循环。我编辑了我的回复。在计算每个点到集群的距离(使用 2 范数)后,您可以计算所有点的距离的平均值或总和。
猜你喜欢
  • 2020-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多