How can I convert a 3D matrix to 2D matrix in the way specified in this post

4 vues (au cours des 30 derniers jours)
Hello, I am new to Matlab and am having a bit of trouble shaping a 3D matrix in a certain way.
For example, I would like to convert 3D matrix M:
val(:,:,1) =
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
val(:,:,2) =
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
val(:,:,3) =
3 3 3 3 3
3 3 3 3 3
3 3 3 3 3
3 3 3 3 3
into the following 2D matrix:
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
3 3 3 3 3
3 3 3 3 3
3 3 3 3 3
3 3 3 3 3
Using reshape I can convert the matrix into a 2D form, however the end result (which I don't want) is similar to this:
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
What is an efficient way to achieve the desired 2D matrix orientation?
EDIT:
While the example above shows val(:,:,1) through val(:,:,3), the script is meant to allow from val(:,:,1) through val(:,:,n) where n can be any number I need it to be.
Additionally, the dimensions of each "val" are 4x5 in this example, but could be any dimension, according to variables specified.
Essentially, I would like an (a,b,c) matrix to become an (a,b) matrix, where the (a,b) sections are stacked in order from 1 to c.
Thank you, and sorry for the lack of clarity in the original question.

Réponse acceptée

Roger Stafford
Roger Stafford le 4 Juin 2016
Modifié(e) : Roger Stafford le 4 Juin 2016
result = reshape(permute(val,[1,3,2]),[],size(val,2)); % <-- Corrected
  4 commentaires
Neuro Guy
Neuro Guy le 4 Juin 2016
This worked perfectly!
Thank you.
Akash kumar
Akash kumar le 21 Fév 2021
Modifié(e) : Akash kumar le 21 Fév 2021
1.) If you want to add matrix in Vertical direction [Roger stafford:- I support it.]
result = reshape(permute(val,[1,3,2]),[],size(val,2));
2.) If you want to add matrix in Horizontal direction then
M=reshape(A,size_of_the_row,[]) % But size of the each row is equal.
for example:- According to the "Neuro Guy" first post:- each submatrix row size=4
then, M=reshape(A,4,[]); then output is (According to the 2.) point)
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3
(According to the 1.) point):- Output is
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
3 3 3 3 3
3 3 3 3 3
3 3 3 3 3
3 3 3 3 3

Connectez-vous pour commenter.

Plus de réponses (2)

Star Strider
Star Strider le 4 Juin 2016
This works:
val = cat(3, ones(4,5), ones(4,5)*2, ones(4,5)*3);
val2D = reshape(val, 5, [])'
val2D =
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
3 3 3 3 3
3 3 3 3 3
3 3 3 3 3
3 3 3 3 3
  6 commentaires
Neuro Guy
Neuro Guy le 6 Juin 2016
This will work great. Thank you for the wisdom.
Star Strider
Star Strider le 6 Juin 2016
My pleasure.
I would have preferred that you Accept my Answer.

Connectez-vous pour commenter.


Azzi Abdelmalek
Azzi Abdelmalek le 4 Juin 2016
a=permute(val,[2 1 3])
out=a(:,:)'
  1 commentaire
Neuro Guy
Neuro Guy le 4 Juin 2016
I'm sorry, I should have clarified. I will edit the original question to reflect this:
While the example above shows val(:,:,1) through val(:,:,3), the script is meant to allow from val(:,:,1) through val(:,:,n) where n can be any number I need it to be.
Additionally, the dimensions of each "val" are 4x5 in this example, but could be any dimension, according to variables specified.
Essentially, I would like an (a,b,c) matrix to become an (a,b) matrix, where the (a,b) sections are stacked in order from 1 to c.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by