Suppose i have matrix the first and second column represent x,y-coordinates and third column is cluster id

2 views (last 30 days)
95.0129 5.7891 3.0000
23.1139 35.2868 1.0000
60.6843 81.3166 2.0000
48.5982 0.9861 3.0000
89.1299 13.8891 3.0000
76.2097 20.2765 3.0000
45.6468 19.8722 3.0000
1.8504 60.3792 1.0000
82.1407 27.2188 3.0000
44.4703 19.8814 3.0000
61.5432 1.5274 3.0000
79.1937 74.6786 2.0000
92.1813 44.5096 2.0000
73.8207 93.1815 2.0000
17.6266 46.5994 1.0000
40.5706 41.8649 1.0000
93.5470 84.6221 2.0000
91.6904 52.5152 2.0000
41.0270 20.2647 3.0000
89.3650 67.2137 2.0000
I want create the matrix of individual cluster id with its x,y coordinates, for example cluster 1; c(1)= [23.1139 35.2868; 1.8504 60.3792; 17.6266 46.5994; 40.5706 41.8649]

Accepted Answer

Stephen23
Stephen23 on 15 Aug 2018
Edited: Stephen23 on 15 Aug 2018
Method One: accumarray:
>> R = 1:size(M,1);
>> C = accumarray(M(:,3),R(:),[],@(r){M(r,1:2)});
>> C{1}
ans =
23.1139 35.2868
1.8504 60.3792
17.6266 46.5994
40.5706 41.8649
>> C{2}
ans =
60.6843 81.3166
79.1937 74.6786
92.1813 44.5096
73.8207 93.1815
93.5470 84.6221
91.6904 52.5152
89.3650 67.2137
>> C{3}
ans =
95.0129 5.7891
48.5982 0.9861
89.1299 13.8891
76.2097 20.2765
45.6468 19.8722
82.1407 27.2188
44.4703 19.8814
61.5432 1.5274
41.0270 20.2647
Method Two: arrayfun:
>> fun = @(r) M(M(:,3)==r,1:2);
>> C = arrayfun(fun, unique(M(:,3)), 'uni',false);
>> C{1}
ans =
23.1139 35.2868
1.8504 60.3792
17.6266 46.5994
40.5706 41.8649
>> C{2}
ans =
60.6843 81.3166
79.1937 74.6786
92.1813 44.5096
73.8207 93.1815
93.5470 84.6221
91.6904 52.5152
89.3650 67.2137
>> C{3}
ans =
95.0129 5.7891
48.5982 0.9861
89.1299 13.8891
76.2097 20.2765
45.6468 19.8722
82.1407 27.2188
44.4703 19.8814
61.5432 1.5274
41.0270 20.2647
  2 Comments
Juma Ally
Juma Ally on 15 Aug 2018
Thank you for the answer; As i consider x,y-coordinates represent the location of the nodes in x,y-plan and i assume the nodes in each cluster are correlated based on overlapping. I want to compute the correlation coefficient by first compute the euclidean distance between the nodes and then using exponential model to compute correlation coefficient for example c(1,1) = exp(-d(1,1)) then i can get matrix for each cluster. Thank you

Sign in to comment.

More Answers (0)

Categories

Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!