tags:

views:

143

answers:

4

Hi, 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

A: 

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.

Pieter
Thanks, I guess this is the solution to extract them from the 2-D matrix based on cluster matrix. The problem is the number of clusters, in other words, the range of numbers in the cluster array is not known. and in each run they will be different. Sometime 5,6,7,10,..... I have to find a way so I can dynamically alot matrixes to extract data to them from DATA matrix....
Hossein
Assuming they get number incrementally and without gaps, call `max` on your index vector. You can loop over all possibles index values in that way, performing your desired operations on each cluster.
Pieter
agree. but I want to save them in different matrixes how do I define matrixes dynamicaly?
Hossein
Use multi-dimensional arrays.
Pieter
+1  A: 

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
gnovice
+1  A: 

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.

Jonas
A: 

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.

Hossein
There are a few problems with your code: 1) If `0` is a value in `class`, it will be ignored. 2) You should avoid using the word `class` for a variable name, since it is a built-in function in MATLAB. 3) You don't really have to use the variable `h`. You can use the loop variable `i` instead.
gnovice
Oh, yes thank you, I didn't notice that i will fix it, also I do this because I don't have cluster 0. But again thank you for your comments.
Hossein

related questions