How to convert a 3D matrix into 2D matrix?

조회 수: 146 (최근 30일)
akshay raj
akshay raj 2016년 5월 11일
댓글: Steven Lord 2023년 5월 28일
Hi,
I am trying to convert 3000x64x278 into 3000*64 rows and 278 columns.
I do know that it can be done something like this:
for example A is of 3000x64x278 matrix so I can call its first matrix as
B=A(:,:,1);
to change it into 3000*64 that means every column under one column I can do
B=B(:);
so There are more 277 columns to fill, how should I do that?
Thanks.

채택된 답변

Stephen23
Stephen23 2016년 5월 11일
편집: Stephen23 2016년 5월 11일
Use reshape.
>> A = reshape(1:4*3*2,4,3,2) % array of size (4,3,2)
A(:,:,1) =
1 5 9
2 6 10
3 7 11
4 8 12
A(:,:,2) =
13 17 21
14 18 22
15 19 23
16 20 24
>> S = size(A);
>> M = reshape(A,[S(1)*S(2),S(3)]) % matrix of size (4*3,2)
M =
1 13
2 14
3 15
4 16
5 17
6 18
7 19
8 20
9 21
10 22
11 23
12 24
  댓글 수: 5
Ioannis Matthaiou
Ioannis Matthaiou 2021년 12월 23일
Hi,
Solution update:
C=[];
for i = 1 : size(A,3)
C = [C; A(:,:,i)];
end
C =
1 2
3 4
5 6
7 8
9 10
11 12
The matrix grows inside the loop, which is okay.
Thanks.
Yiannis
Stephen23
Stephen23 2021년 12월 23일
편집: Stephen23 2021년 12월 23일
"The matrix grows inside the loop, which is okay."
Not really, see:
In contrast, reshaping an array is extremely efficient (no data gets moved).

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

추가 답변 (1개)

jinhu
jinhu 2023년 5월 28일
The order of three-dimensional arrays in MATLAB is: row, column, and page. A two-dimensional array only has rows and columns. If two values are assigned between them, there is a dimensionality reduction issue that needs to be noted.
For example, A3 is a three-dimensional array, where A3 (:,:, 1)=[1,2,3; 4,5,6]; A3 (:,:, 2)=[7,8,9; 10,11,12];
So in the assignment of A2=A3 (:,:, 1), the result A2 is a two-dimensional matrix (a two-dimensional array) (a matrix of 2X3).
In the assignment of A2=A3 (1,:,:), the result A2 is a three-dimensional matrix (1X3X2 matrix).
Essentially, they should all be a two-dimensional matrix. Why does A2 become a three-dimensional matrix in the latter assignment, while the former is two-dimensional?
That is to say, the former should also be considered three-dimensional, how can it be reduced to two-dimensional, while the latter cannot be reduced to two-dimensional?
The main reason is that in a three-dimensional matrix, the first dimension represents rows, the second dimension represents columns, and the third dimension represents pages. When the third dimension is 1, it represents only 1 page, naturally reducing to 2D. When the first dimension is 1, it represents only one row, but each page has one, so from the perspective of the room, it is not reduced to two-dimensional.
The order of three-dimensional arrays in MATLAB is: row, column, and page. A two-dimensional array only has rows and columns. If two values are assigned between them, there is a dimensionality reduction issue that needs to be noted.
For example, A3 is a three-dimensional array, where A3 (:,:, 1)=[1,2,3; 4,5,6]; A3 (:,:, 2)=[7,8,9; 10,11,12];
So in the assignment of A2=A3 (:,:, 1), the result A2 is a two-dimensional matrix (a two-dimensional array) (a matrix of 2X3).
In the assignment of A2=A3 (1,:,:), the result A2 is a three-dimensional matrix (1X3X2 matrix).
Essentially, they should all be a two-dimensional matrix. Why does A2 become a three-dimensional matrix in the latter assignment, while the former is two-dimensional?
That is to say, the former should also be considered three-dimensional, how can it be reduced to two-dimensional, while the latter cannot be reduced to two-dimensional?
The main reason is that in a three-dimensional matrix, the first dimension represents rows, the second dimension represents columns, and the third dimension represents pages. When the third dimension is 1, it represents only 1 page, naturally reducing to 2D. When the first dimension is 1, it represents only one row, but each page has one, so from a physical perspective, it is not reduced to two-dimensional.
If a 2D result is required in the end, please use reshape processing.
  댓글 수: 1
Steven Lord
Steven Lord 2023년 5월 28일
MATLAB does not display trailing singleton dimensions of an array. But leading singleton dimensions are important.

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

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by