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

116 次查看(过去 30 天)
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