Change a 3D matrix to a stacked 2D matrix

46 次查看(过去 30 天)
I have a matrix of (5*5*14680) dimensions, basically 14680 of five by five matrices, I need to convert this output to a 2D matrix (I believe) so basically all the rows are stacked up, and the new matrix dimension will then be : 73400*5. I tried this out = reshape(permute(in, [1:14680]), [], 5); but the output matrix was did not seem correct, everything was out of order. Any suggestions? Please.

回答(2 个)

Guillaume
Guillaume 2016-5-6
Your permute does not make much sense and implies that there are 14680 dimensions instead of three. Since all the dimensions are listed ordered, it actually does not swap any of them. Here is the way to do it with reshape:
out = reshape(permute(in, [2 1 3]), size(in, 2), [])'
You need to transpose rows and columns before and after the reshape since matlab operates column-wise.
If thinking in terms of reshape and permute hurts your head (it does for me), it is simpler to go through a cell array and plain concatenation. The downside is speed:
out = num2cell(in, [1 2]); %split into cell array keeping dimensions 1 and 2 together
out = vertcat(out{:}) %concatenate all the cells vertically

Azzi Abdelmalek
Azzi Abdelmalek 2016-5-6
out=reshape(permute(a,[2 1 3]),size(a,2),[])'
  2 个评论
Amine Ben Ayara
Amine Ben Ayara 2016-5-6
Azzi, I do not understand what the [2 1 3] are actually specifying? I do not want the order of my rows to change at all from the original matrix. this will not affect my order, right? Thank you
Azzi Abdelmalek
Azzi Abdelmalek 2016-5-6
Look at the final result, not just the intermediate!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by