I have a matrix of data which is the coordinates of some points and coordinates of 5 clusters
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'];
I want to use norm function to determine the distances from the centers of 5 known clusters which are the above coordinates to my data. How could I do that?
The funtion norm(X)
is the same as norm(X,2)
. Matlab uses the 2-norm (Euclidean distance) by default.
Using your code to begin:
% 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);
Then use for loops for all clusters (n_clusters
) and for each point (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,:)-Coordinates_Of_Clusters(n,:), 2);
end
end