For loop to calculate distance between a 5d dimensional data point and another matrix

3 visualizaciones (últimos 30 días)
I am trying to create a for loop to calculate the distance between my matrix 'T' and another matrix 'centroids'.
I dont know how to add in another loop to include the matrix 'centroids' Can someone help me. please see the code i have completed so far. If someone could explain where i am going wrong and tell me what to add to fix it, then i would really appreciate it.
T1 = [1 4 3 100 12 1000 25 32 500 700 20 9000 25 650 40 3 67 2 305 80 200 50 2 15 4]
T2 = [1 6 2 120 14 900 25 35 550 650 23 7500 25 700 43 6 72 8 360 70 220 60 1 14 8]
T3 = [1.5 5 7 150 13 550 1 40 520 600 27 5500 27 900 44 12 75 9 360 50 190 50 4 13 6]
T4 = [122 120 120 170 140 700 100.5 134 570 600 125 6100 125 1050 122 130 187 109 460 145 330 160 105 113 104]
T5 = [25 25 30 40 50 600 6 33 500 600 26 5500 26 1150 14 32 126 8 360 28 230 60 2 13 5]
T=[T1;T2;T3;T4;T5]
display (T) % (notes: T is a 5*25 matrix)
centroids=[T1(1:3);T2(1:3);T3(1:3);T4(1:3);T5(1:3)] (notes: centroids is the first 3 colums of T)
display (centroids)
T=[T1;T2;T3;T4;T5]
[m n] = size(T);
for i=1:(n-1)
b = T(:,i);
c = T(:,i+1);
d = 0;
for j=1:m
d = d + (b(j)-c(j))^2;
end
e(i)=sqrt(d);
end
display(e)
  5 comentarios
Deborah O'Connor
Deborah O'Connor el 23 de Jun. de 2020
thats what i dont know. here is the exact question i am being asked:
'use a for loop to look at each column of T in turn
Each of the 25 columns describes a 5 dimensional data point
complete the code so that:
inside the for-loop you calculate the distance between the 5 dimensional data point and each of the 3 centroids
then assign to the corresponding component of the vector cluster_assigned either the number 1, 2 or 3 depending on which cluster is nearest

Iniciar sesión para comentar.

Respuestas (2)

the cyclist
the cyclist el 23 de Jun. de 2020
The distance from the i-th point of T to the j-th centroid is
sqrt(sum((T(:,i) - centroids(:,j)).^2)); % Check my formula here, but I think that's right.
So, if that's the distance for one T point, from one centroid, you just need to loop over all points:
for i = 1:size(T,2)
for j = 1:size(centroids,2)
% ...
end
end
I don't think you need your other loop down the first dimension of T, because that is covered by the sum command above, which is vectorized.
That's a pretty big hint, so I hope that is enough to get you going.
  7 comentarios
Deborah O'Connor
Deborah O'Connor el 24 de Jun. de 2020
I hope I did'nt offend you. It is me who is not sure what i am doing. Big big apologies if i offended you, it was not intentional. I am extremely grateful for your help. Thank you so much for explaining this for me. I cannot thank you enough.
the cyclist
the cyclist el 24 de Jun. de 2020
I'm not offended at all. I'm sorry if I gave that impression.

Iniciar sesión para comentar.


Deborah O'Connor
Deborah O'Connor el 24 de Jun. de 2020
I am happy you are not offended. Just making sure. I might need your help if i cant work out how to do the next part (assigning the results to clusters 1 2 and 3). Maths is my weakpoint so it takes me a long time to get my head around things

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by