Split matrix into N Equal Parts by rows

30 Ansichten (letzte 30 Tage)
Inna Pelloso
Inna Pelloso am 3 Sep. 2022
Bearbeitet: Matt J am 16 Aug. 2024
Hi,
I have an Nx10 matrix. How can I split this into three equally sized matrices (by number of rows) ? Is this something the reshape command can handle?
Thank you!
IP

Akzeptierte Antwort

Matt J
Matt J am 3 Sep. 2022
Bearbeitet: Matt J am 3 Sep. 2022
If N is a multiple of 3, you can download blkReshape,
N=12; M=10;
A=reshape(1:N*M,N,M),
B=blkReshape(A,[N/3,M],1,1,[]),
A =
1 13 25 37 49 61 73 85 97 109
2 14 26 38 50 62 74 86 98 110
3 15 27 39 51 63 75 87 99 111
4 16 28 40 52 64 76 88 100 112
5 17 29 41 53 65 77 89 101 113
6 18 30 42 54 66 78 90 102 114
7 19 31 43 55 67 79 91 103 115
8 20 32 44 56 68 80 92 104 116
9 21 33 45 57 69 81 93 105 117
10 22 34 46 58 70 82 94 106 118
11 23 35 47 59 71 83 95 107 119
12 24 36 48 60 72 84 96 108 120
B(:,:,1) =
1 13 25 37 49 61 73 85 97 109
2 14 26 38 50 62 74 86 98 110
3 15 27 39 51 63 75 87 99 111
4 16 28 40 52 64 76 88 100 112
B(:,:,2) =
5 17 29 41 53 65 77 89 101 113
6 18 30 42 54 66 78 90 102 114
7 19 31 43 55 67 79 91 103 115
8 20 32 44 56 68 80 92 104 116
B(:,:,3) =
9 21 33 45 57 69 81 93 105 117
10 22 34 46 58 70 82 94 106 118
11 23 35 47 59 71 83 95 107 119
12 24 36 48 60 72 84 96 108 120
  2 Kommentare
Inna Pelloso
Inna Pelloso am 3 Sep. 2022
Bearbeitet: Inna Pelloso am 3 Sep. 2022
Wow! This is a seriously useful function! I wonder why there isn't a built in function like that!
Is there any way to add the last rows that are greater that the last multiple (e.g. the last two rows if I have 302 rows) to the first/middle/last block? This is a possible additional feature for this function.
Matt J
Matt J am 3 Sep. 2022
You can pad with additional rows, e.g.,
N=ceil(N/3)*3;
A(end+1:N,:)=nan;

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Walter Roberson
Walter Roberson am 4 Sep. 2022
pieces = 3;
N = size(A, 1);
part_sizes = floor(N/pieces) * [1 1];
part_sizes(end+1) = N - sum(part_sizes);
C = mat2cell(A, part_sizes, size(A,2));
C is now a cell array in which the rows are equally divided as possible, with the last block being shorter if necessary.
  2 Kommentare
Gorkem
Gorkem am 5 Mär. 2024
Thanks for the code. I was having trouble with the last block being too small compared to other blocks so I distributed residuals of the last block through the equally spaced blocks. It's not the best but here is the version I updated in this way:
pieces = 100;
N = size(A, 1);
part_sizes = floor(N/pieces) * [ones(1:pieces)];
Res = N - sum(part_sizes);
part_sizes(1:Res) = part_sizes(1:Res) + 1;
C = mat2cell(A, part_sizes, size(A,2));
AlexDiz
AlexDiz am 16 Aug. 2024
it's [ones(1, pieces)] instead of [ones(1:pieces)]
but it works perfectly !
thanks

Melden Sie sich an, um zu kommentieren.


Matt J
Matt J am 16 Aug. 2024
Bearbeitet: Matt J am 16 Aug. 2024
You can also use mat2tiles,
which doesn't require even multiples of the block size. However, it is never advisable to use cell arrays when you don't have to.
A=rand(14,6);
B=mat2tiles(A,[4,inf]) %group every 4 rows
B = 4x1 cell array
{4x6 double} {4x6 double} {4x6 double} {2x6 double}

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by