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

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.

 Respuesta aceptada

Stephen23
Stephen23 el 11 de Mayo de 2016
Editada: Stephen23 el 11 de Mayo de 2016
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 comentarios

akshay raj
akshay raj el 11 de Mayo de 2016
Yes. Thank you very much.
Ioannis Matthaiou
Ioannis Matthaiou el 23 de Dic. de 2021
Hi,
If we have the following:
A(:,:,1)=[1, 2; 3, 4]; A(:,:,2)=[5, 6; 7, 8]; A(:,:,3)=[9, 10; 11, 12];
Then using:
C=reshape(A,size(A,3)*size(A,1),size(A,2));
we get:
C =
1 6
3 8
2 9
4 11
5 10
7 12
The problem is that, if we want a different order, such that:
C=
1 2
3 4
5 6
7 8
9 10
11 12
That is, bringing the 3rd dimension, to form each row, leaving each column as it is. How can this be done?
Thanks.
Yiannis
DGM
DGM el 23 de Dic. de 2021
Editada: DGM el 23 de Dic. de 2021
Since reshape() works columnwise, it's often necessary to orient the array using permute().
A(:,:,1) = [1, 2; 3, 4];
A(:,:,2) = [5, 6; 7, 8];
A(:,:,3) = [9, 10; 11, 12];
B = reshape(permute(A,[1 3 2]),[],2,1)
B = 6×2
1 2 3 4 5 6 7 8 9 10 11 12
You can think of the elementwise transpose operator as a restricted case of permute that only works on 2D arrays, so sometimes it can be used too.
Ioannis Matthaiou
Ioannis Matthaiou el 23 de Dic. de 2021
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 el 23 de Dic. de 2021
Editada: Stephen23 el 23 de Dic. de 2021
"The matrix grows inside the loop, which is okay."
Not really, see:
In contrast, reshaping an array is extremely efficient (no data gets moved).

Iniciar sesión para comentar.

Más respuestas (1)

jinhu
jinhu el 28 de Mayo de 2023

0 votos

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 comentario

Steven Lord
Steven Lord el 28 de Mayo de 2023
MATLAB does not display trailing singleton dimensions of an array. But leading singleton dimensions are important.

Iniciar sesión para comentar.

Categorías

Productos

Etiquetas

Preguntada:

el 11 de Mayo de 2016

Comentada:

el 28 de Mayo de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by