Keeping columns names but replacing value for iteration

3 views (last 30 days)
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;
%getting initial centroid K coordinates
K = A(1:num_K,2:3);
for h = [1:iter]
%finding Euc.distance from K to r
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]
% unrelated = unrelated code that changes the value of column 'dist1', 'dist2', 'dist3' every iteration
% ......
% ......
% ......
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!

Accepted Answer

Peter Perkins
Peter Perkins on 20 Nov 2020
You are horzcat'ing tables, and table variable names must be unique. But your two tables both have variables named dist1, dist2, dist3. So you have two choices:
1) modify the var names in the new table to dist4, dist5, dist6, then dist7, dist8, dist9, and so on
2) rather than horzcat'ing, create nested tables, like this:
>> t1 = array2table(rand(5,2),'VariableNames',["d1" "d2"]);
>> t2 = array2table(rand(5,2),'VariableNames',["d1" "d2"]);
>> t = table([1;2;3;4;5],t1,t2,'VariableNames',["X" "T1" "T2"])
t =
5×3 table
X T1 T2
d1 d2 d1 d2
_ ___________________ __________________
1 0.97235 0.70763 0.45469 0.79374
2 0.29313 0.52507 0.42832 0.64548
3 0.2155 0.27734 0.81563 0.93903
4 0.46152 0.017199 0.33228 0.17186
5 0.65587 0.75829 0.76782 0.51153
Or are you trying to overwrite the old dist# variables with the new ones?
A(:,["dist1" "dist2" "dist3"]) = dist_T
  1 Comment
Christoper Angelo Malayan
Christoper Angelo Malayan on 21 Nov 2020
Got some ideas on how to implement it excatly how I want thanks to this.
Thank you, Peter!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!