Clear Filters
Clear Filters

How to reshape a matrix?

2 views (last 30 days)
Priyabrata Das
Priyabrata Das on 2 Oct 2020
Commented: Ameer Hamza on 3 Oct 2020
Suppose I have a 2D matrix (6,3) as follows
A
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
16 17 18
Now I want to reshape into one 3D matrix (3,3,2) like as follows:
A_3D(:, :, 1)
1 2 3
4 5 6
7 8 9
A_3D(:, :, 2)
10 11 12
13 14 15
16 17 18
How I can achieve that with an efficient way?
Thank you
  1 Comment
VBBV
VBBV on 2 Oct 2020
% if true
% code
% end
A_3D = reshape(A,3,3,[])

Sign in to comment.

Accepted Answer

Ameer Hamza
Ameer Hamza on 2 Oct 2020
Edited: Ameer Hamza on 2 Oct 2020
Combination of reshape and permute
permute(reshape(A, 3, size(A,1)/3, []), [1 3 2])
  4 Comments
Priyabrata Das
Priyabrata Das on 3 Oct 2020
It is working fine. Now I have to understand the logic behind this line of code. Thank you very much.
Ameer Hamza
Ameer Hamza on 3 Oct 2020
I am glad to be of help. You can run each part one by one and see the output. That will help in understanding.

Sign in to comment.

More Answers (0)

Categories

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

Community Treasure Hunt

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

Start Hunting!