Hi all. I'm new at MATLAB and in coding in general. I'm practicing with some modified K-means clustering and I encountered this problem when iterating. I can't seem to figure it out. I uploaded the sample data named 'CCP.xlsx'. This is my code:
A = readtable('CCP.xlsx');
A = sortrows(A,'d','descend');
C = 64;
num_r = length(A.Customer);
num_K = round(sum(A.d / C));
iter = 10;
K = A(1:num_K,2:3);
for h = [1:iter]
for t = [1:num_K]
for i = [1:num_r]
dist(i,t) = sqrt((K.x(t) - A.x(i))^2 + (K.y(t) - A.y(i))^2);
end
end
dist_T = array2table(dist)
A = [A dist_T]
end
When I ran this, it outputs this for the 1st iteration:
A =
10×7 table
Customer x y d dist1 dist2 dist3
________ __ __ __ ______ ______ ______
4 2 19 30 0 16.279 3.1623
2 18 16 30 16.279 0 17
9 1 16 22 3.1623 17 0
7 14 17 20 12.166 4.1231 13.038
8 2 6 19 13 18.868 10.05
5 5 12 17 7.6158 13.601 5.6569
1 13 6 15 17.029 11.18 15.62
3 13 6 15 17.029 11.18 15.62
6 16 9 13 17.205 7.2801 16.553
10 3 6 10 13.038 18.028 10.198
Iteration 1 , success!
But for the 2nd iteration (new values are derived from 'unrelated'), it seems that it wants to add new and separate columns at the end of table A that is also named column 'dist1', 'dist2', 'dist3' and as a result, it outputs this:
dist_T =
10×3 table
dist1 dist2 dist3
______ ______ ______
6.5192 14.866 10.912
15.89 2.8284 13.103
3.8079 15.133 8.9376
12.349 3.6056 10.615
6.5192 16.125 5.7689
2.5495 11.18 3.3287
12.349 8.544 6.9771
12.349 8.544 6.9771
13.946 5 9.2022
6.5192 15.264 4.9679
Error using CCP4 (line 27)
Duplicate table variable name: 'dist1'.
My end-goal is that no matter how many column 'dist#' there is, the code can still run with no problem.
That said, is there a way to just replace the values for columns 'dist1', 'dist2', 'dist3' for every following iteration so that they remain intact as column #5 ,6, 7 for table A?
I've been trying other methods but they all just add new columns at the end of table A named column 'dist1_1', 'dist2_1', 'dist3_1', etc ....
Any help or tips are greatly appreciated. Thank you!
0 Comments
Sign in to comment.