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

조회 수: 3(최근 30일)
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

표시 이전 댓글 수: 2
KSSV 23 Jun 2020
T=[T1;T2;T3;T4;T5] % 5*25 size
C = T(:,1:3) ; % centroids 5*3 size
P = T(:,4:end) ; % points 5*22
There is missmatch in dimensions......how you would expect to get distance?
Deborah O'Connor 23 Jun 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
Deborah O'Connor 23 Jun 2020
Can anyone help me with the above query? thanks

### 답변(2개)

the cyclist 23 Jun 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

표시 이전 댓글 수: 4
the cyclist 24 Jun 2020
Take the code apart, from the "inside out":
T(:,i)
is the 5-d coordinate vector of the i'th point. Similarly,
centroids(:,j)
is the coordinate vector of the j'th centroid.
T(:,i) - centroids(:,j)
is the vector of the differences of those coordinates.
(T(:,i) - centroids(:,j)).^2
is the square of each of those differences.
sum((T(:,i) - centroids(:,j)).^2)
is the sum of those squares. Finally,
sqrt(sum((T(:,i) - centroids(:,j)).^2))
is the square root of that sum. That's the distance formula (assuming I did not make some silly mistake). It is the 5-d equivalent of the formula
sqrt( (x1-x2).^2 + (y1-y2).^2 ) % distance formula in two dimensions
Deborah O'Connor 24 Jun 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 24 Jun 2020
I'm not offended at all. I'm sorry if I gave that impression.