3 views (last 30 days)

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)

the cyclist
on 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.

the cyclist
on 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

Opportunities for recent engineering grads.

Apply Today
## 5 Comments

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/553243-for-loop-to-calculate-distance-between-a-5d-dimensional-data-point-and-another-matrix#comment_910153

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/553243-for-loop-to-calculate-distance-between-a-5d-dimensional-data-point-and-another-matrix#comment_910153

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/553243-for-loop-to-calculate-distance-between-a-5d-dimensional-data-point-and-another-matrix#comment_910162

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/553243-for-loop-to-calculate-distance-between-a-5d-dimensional-data-point-and-another-matrix#comment_910162

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/553243-for-loop-to-calculate-distance-between-a-5d-dimensional-data-point-and-another-matrix#comment_910186

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/553243-for-loop-to-calculate-distance-between-a-5d-dimensional-data-point-and-another-matrix#comment_910186

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/553243-for-loop-to-calculate-distance-between-a-5d-dimensional-data-point-and-another-matrix#comment_910201

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/553243-for-loop-to-calculate-distance-between-a-5d-dimensional-data-point-and-another-matrix#comment_910201

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/553243-for-loop-to-calculate-distance-between-a-5d-dimensional-data-point-and-another-matrix#comment_910255

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/553243-for-loop-to-calculate-distance-between-a-5d-dimensional-data-point-and-another-matrix#comment_910255

Sign in to comment.