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

조회 수: 4 (최근 30일)
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.

채택된 답변

Roger Stafford
Roger Stafford 2016년 6월 4일
편집: Roger Stafford 2016년 6월 4일
result = reshape(permute(val,[1,3,2]),[],size(val,2)); % <-- Corrected
  댓글 수: 4
Akash kumar
Akash kumar 2021년 2월 21일
편집: Akash kumar 2021년 2월 21일
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

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

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

댓글을 달려면 로그인하십시오.


Azzi Abdelmalek
Azzi Abdelmalek 2016년 6월 4일
a=permute(val,[2 1 3])
out=a(:,:)'
  댓글 수: 1
Neuro Guy
Neuro Guy 2016년 6월 4일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by