微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在MATLAB中使用规范函数

如何解决在MATLAB中使用规范函数

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

data = [randi(100,100,1),randi(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,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 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,2);

    end
end

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。