简体   繁体   English

MATLAB:将“ vertcat”反转(将2D转换为3D)

[英]MATLAB: `vertcat` reversed (2D into 3D)

I'm looking for an efficient way to split a huge 2D matrix into several blocks and transfer these blocks into the 3rd dimension. 我正在寻找一种有效的方法,将巨大的2D矩阵拆分为几个块,并将这些块转移到第3维。

Take mat_orig ( 100x10 ). 就拿mat_orig100x10 )。 After every other 20 rows, I need to make a cut and put the resulting block into the 3rd dimension. 每隔20行之后,我需要进行切割并将生成的块放入第3维。 That is, my 100x10 matrix must result in a 20x10x5 matrix. 也就是说,我的100x10矩阵必须导致20x10x5矩阵。

My solution involves mat2cell, permute, and cell2mat. 我的解决方案涉及mat2cell,permute和cell2mat。 It's working, however, I'm working on quite some large matrices. 它正在工作,但是,我正在处理许多大型矩阵。 I am wondering if somebody feels challenged and can provide a more sophisticated (faster) solution? 我想知道是否有人会感到挑战并且可以提供更复杂(更快)的解决方案?

mat_orig = reshape(1:1000, 10, 100)';
mat_len = 20;
num_pages = size(mat_orig, 1) / mat_len;

tic;
mat_splitted = cell2mat(permute(mat2cell(mat_orig, ones(1, num_pages) * 
size(mat_orig, 1) / num_pages, 10), [3, 2,1]));
toc (% Elapsed time is 0.108561 seconds.)



mat_orig = reshape(1:100000000, 10, 10000000)';
mat_len = 10000;
num_pages = size(mat_orig, 1) / mat_len;

tic;
mat_splitted = cell2mat(permute(mat2cell(mat_orig, ones(1, num_pages) * 
size(mat_orig, 1) / num_pages, 10), [3, 2,1]));
toc (% Elapsed time is 4.361126 seconds.)

You can avoid cell2mat : 您可以避免cell2mat

N = 20; % number of rows per block
mat_split = permute(reshape(mat_orig, N, [], size(mat_orig,2)), [1 3 2]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM