How to convert a 3D matrix into 2D matrix?
154 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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.
0 commentaires
Réponse acceptée
Stephen23
le 11 Mai 2016
Modifié(e) : Stephen23
le 11 Mai 2016
>> 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 commentaires
Ioannis Matthaiou
le 23 Déc 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
Plus de réponses (1)
jinhu
le 28 Mai 2023
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 commentaire
Steven Lord
le 28 Mai 2023
MATLAB does not display trailing singleton dimensions of an array. But leading singleton dimensions are important.
Voir également
Catégories
En savoir plus sur Matrices and Arrays dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!