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?
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);
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:
For instance, I ran this on X = eye(7) and got:
Y =
[1x7 double]
...
[1x7 double]
[0x7 double]
[0x7 double]
[0x7 double]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With