Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I divide a matrix into unequally-sized submatrices?

I am wondering if it is possible to use the mat2cell function to divide an MxN matrix into 10 submatrices with the same column size, N, and approximately the same row size ~M/10? If mod(M, 10) == 0 then all submatrices will have the same size, otherwise a few matrices will have +/-1 row. Is this possible via the mat2cell function?

For reference, if the row sizes are all the same it's fairly straightforward, as explained here:
How to divide a matrix into equals parts?

like image 818
C. Reed Avatar asked Dec 28 '10 22:12

C. Reed


2 Answers

Here's a simple solution using the functions linspace, round, and diff:

[M, N] = size(mat);  % Matrix size
nSub = 10;           % Number of submatrices
cMat = mat2cell(mat, diff(round(linspace(0, M, nSub+1))), N);

This approach will distribute extra rows in a more uniform fashion across the resulting cells of the cell array. Note these outputs that you will get when applying the above using mat = magic(5); (left) and mat = magic(13); (right):

cMat =              cMat = 

    [1x5 double]        [1x13 double]
    [0x5 double]        [2x13 double]
    [1x5 double]        [1x13 double]
    [0x5 double]        [1x13 double]
    [1x5 double]        [2x13 double]
    [0x5 double]        [1x13 double]
    [1x5 double]        [1x13 double]
    [0x5 double]        [1x13 double]
    [1x5 double]        [2x13 double]
    [0x5 double]        [1x13 double]

If you'd prefer a random distribution of extra rows, you can use randperm like so:

subSizes = diff(round(linspace(0, M, nSub+1)));
cMat = mat2cell(mat, subSizes(randperm(nSub)), N);
like image 57
gnovice Avatar answered Nov 15 '22 08:11

gnovice


This is possible and is similar to the link you provided, but you need to decide how you want to divide up the 'leftover' rows when M mod 10 is not 0 and what you will do if there are fewer than 10 rows to begin with. The following should work if the listed assumptions are valid:

[M,N] = size(X);
Y = mat2cell(X, [repmat(ceil(M/10),[1 mod(M,10)]) ...
                 repmat(floor(M/10),[1 10-mod(M,10)])], N); 

Assumptions:

  1. You will have >= 10 rows (or you don't mind having 0xN arrays)
  2. You are content to have the additional rows divided among the first matrices - i.e. if you have 13 rows, then you have 3 consecutive matrices with 2 rows, followed by 7 matrices of 1 row.

For instance, I ran this on X = eye(7) and got:

Y = 
 [1x7 double]
   ...
 [1x7 double]
 [0x7 double]
 [0x7 double]
 [0x7 double]
like image 43
sage Avatar answered Nov 15 '22 06:11

sage