split long 2D matrix into the third dimension
Here's a one-line solution using reshape
and permute
:
C = 3; % Number of columns
R = 6; % Number of rows
newR = 2; % New number of rows
A = randi(10, [R C]); % 6-by-3 array of random integers
B = permute(reshape(A.', [C newR R/newR]), [2 1 3]);
This of course requires that newR
divides evenly into R
.
Here's a one-liner with reshape
and permute
, but without transposing the input array -
out = permute(reshape(A,newR,size(A,1)/newR,[]),[1 3 2]);
, where newR
is the number of rows in the 3D
array output.
Benchmarking
This section compares the proposed aproach in this post against the other solution with reshape, permute & transpose
on performance. The datasizes are inflated proportionality to the ones listed in the question. Thus, A
is 60000 x 300
sized and we would split it such that the 3D
output would have 200 rows
and thus dim-3 would have 300
entries.
Benchmarking code -
%// Input
A = randi(10, [60000 300]); %// 2D matrix
newR = 200; %// New number of rows
%// Warm up tic/toc.
for k = 1:50000
tic(); elapsed = toc();
end
N_iter = 5; %// Number of iterations for each approach to run with
disp('---------------------- With PERMUTE, RESHAPE & TRANSPOSE')
tic
for iter = 1:N_iter
[R,C] = size(A);
B = permute(reshape(A',[C newR R/newR]),[2 1 3]); %//'
end
toc, clear B R C iter
disp('---------------------- With PERMUTE & RESHAPE')
tic
for iter = 1:N_iter
out = permute(reshape(A,newR,size(A,1)/newR,[]),[1 3 2]);
end
toc
Output -
---------------------- With PERMUTE, RESHAPE & TRANSPOSE
Elapsed time is 2.236350 seconds.
---------------------- With PERMUTE & RESHAPE
Elapsed time is 1.049184 seconds.