Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

splitting a matlab matrix into several equal parts

I have a matrix of size 64500x17. It represents detected texton features that I have to use to find 5 centroids for kmeans.

What I need is:

  • split this matrix into 5 12900x17 matrices
  • find the means
  • concatenate these into a 5x17 matrix to feed in to the start parameter of kmeans.

I know how to do almost everything (cat, kmeans, etc), but I am merely trying to find a method for splitting the matrix into 5 parts, or summing/dividing into the desired size.

I am forbidden from overusing for loops (due to efficiency), unless absolutely necessary.

I can't find any pertinent example in other questions, so if this has been answered, please bear with me.

like image 294
Jacob Moses Avatar asked Dec 06 '12 21:12

Jacob Moses


1 Answers

You can use mat2cell and this oneliner

C = mat2cell(A, repmat(12900, 5, 1), 17);

The second parameter to mat2cell is the row split of the matrix.

Now C is a cell array:

C = 

[12900x17 double]
[12900x17 double]
[12900x17 double]
[12900x17 double]
[12900x17 double]

and the partial matrices can be accessed as

C{1} etc.
like image 141
angainor Avatar answered Nov 03 '22 00:11

angainor