Reputation: 41961
I have a 2D Matrix consisting of some coordinates as below(example): Data(X,Y):
45.987543423,5.35000964
52.987544223,5,98765234
Also I have an array consisting of some integers >=0 , for example: Cluster(M)
2,0,3,1
each of these numbers in this array corresponds with a row of my 2D Matrix above.For example, it says that row one(coordinate) in the Data Matirx belongs to the cluster 2,second row belongs to cluster 0 and so on. Now I want to have each of the datapoint of each cluster in a separate matrix, for example I want to save datapoints belonging to cluster 1 in a separate matrix, cluster 2 in a separate matrix and so on,.... I can do them manually, but the problem is this has to be an automatic extraction. which means that the number of clusters(range of the numbers in the cluster array varies in each run) so I have to have a general algorithm that does this extraction for me. Can someone help me please? thanks
Upvotes: 1
Views: 2576
Reputation: 963
I would either create a 3 dimensional array or table. That way the cluster index would be associated with the cluster. Something like the following construct:
xData = Data(:,1);
yData = Data(:,2);
clusterTable = table(Cluster, xData, yData);
This creates a table with column names and each row having a cluster index and a set of coordinates.
Upvotes: 0
Reputation: 41961
Thanks everyone, I managed to make it work with this code:
noOfClusters = max(cluster); %without noise
for i=1:noOfClusters
C(i,1) = {numData(cluster==i,:)}
end
I assume your codes are much faster,cause you don't use for loops.
Upvotes: 0
Reputation: 74940
You can use ARRAYFUN to distribute the coordinates among different cell arrays.
%# create sample data
clusterIdx = [2,0,3,1,1,1,3,2];
coordinates = rand(8,2);
%# first you get a list of unique cluster indices
clusterIdxUnique = unique(clusterIdx);
%# then you use arrayfun to distribute the coordinates
clusterCell = arrayfun(@(x)coordinates(clusterIdx==x,:),clusterIdxUnique,'UniformOutput',false);
The first element of clusterCell
contains the coordinates corresponding to the first entry in clusterIdxUnique
, etc.
Upvotes: 1
Reputation: 125874
Instead of dynamically creating a bunch of matrices, I would create a cell array with each matrix in a separate cell. Here's one way to do this, using the functions SORT and MAT2CELL:
[cluster,sortIndex] = sort(cluster); %# Sort cluster and get sorting index
data = data(sortIndex,:); %# Apply the same sorting to data
clusterCounts = diff([0 find(diff(cluster)) numel(cluster)]); %# Find size of
%# each cluster
cellArray = mat2cell(data,clusterCounts,2); %# Break up data into matrices,
%# each in a separate cell
Upvotes: 1
Reputation: 2912
I guess this is the solution:
data(cluster == i, :)
where i
is the index of the cluster. Your index matrix is converted to a boolean matrix and then used to index the rows and each selected row is completely added to the resulting matrix.
If this is not what you're looking for, please specify your needs more clearly.
Upvotes: 0