Clear Filters
Clear Filters

Change a 3D matrix to a stacked 2D matrix

46 views (last 30 days)
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.

Answers (2)

Guillaume
Guillaume on 6 May 2016
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 on 6 May 2016
out=reshape(permute(a,[2 1 3]),size(a,2),[])'
  2 Comments
Amine Ben Ayara
Amine Ben Ayara on 6 May 2016
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 on 6 May 2016
Look at the final result, not just the intermediate!

Sign in to comment.

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!