For loop to calculate distance between a 5d dimensional data point and another matrix
1 view (last 30 days)
Show older comments
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 Comments
Answers (2)
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.
7 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!