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

4 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

Roger Stafford
Roger Stafford am 4 Jun. 2016
Bearbeitet: Roger Stafford am 4 Jun. 2016
result = reshape(permute(val,[1,3,2]),[],size(val,2)); % <-- Corrected
  4 Kommentare
Akash kumar
Akash kumar am 21 Feb. 2021
Bearbeitet: Akash kumar am 21 Feb. 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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Star Strider
Star Strider am 4 Jun. 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 Kommentare
Neuro Guy
Neuro Guy am 6 Jun. 2016
This will work great. Thank you for the wisdom.
Star Strider
Star Strider am 6 Jun. 2016
My pleasure.
I would have preferred that you Accept my Answer.

Melden Sie sich an, um zu kommentieren.


Azzi Abdelmalek
Azzi Abdelmalek am 4 Jun. 2016
a=permute(val,[2 1 3])
out=a(:,:)'
  1 Kommentar
Neuro Guy
Neuro Guy am 4 Jun. 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.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by