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

clustering

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]

Best Answer

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