How to reshapre a matrix?

1 vue (au cours des 30 derniers jours)
gsourop
gsourop le 2 Juil 2019
Commenté : KALYAN ACHARJYA le 2 Juil 2019
Hi everyone,
I would like to reshape a matrix, say A:
rng default
A = randn(12,4)
A =
0.5377 -1.3499 0.6715 0.8884
1.8339 3.0349 -1.2075 -1.1471
-2.2588 0.7254 0.7172 -1.0689
0.8622 -0.0631 1.6302 -0.8095
0.3188 0.7147 0.4889 -2.9443
-1.3077 -0.2050 1.0347 1.4384
-0.4336 -0.1241 0.7269 0.3252
0.3426 1.4897 -0.3034 -0.7549
3.5784 1.4090 0.2939 1.3703
2.7694 1.4172 -0.7873 -1.7115
-1.3499 0.7172 -2.9443 -0.1649
3.0349 1.6302 1.4384 0.6277
into 3 bits that will be horizontally combined. So that the output would be a matrix B of dimensions 4x12, such as:
B =
0.5377 0.7254 0.4889 0.3252 0.3188 -0.1241 0.2939 -0.1022 3.5784 0.6715 -1.0689 -0.8649
1.8339 -0.0631 1.0347 -0.7549 -1.3077 1.4897 -0.7873 -0.2414 2.7694 -1.2075 -0.8095 -0.0301
-2.2588 0.7147 0.7269 1.3703 -0.4336 1.4090 0.8884 0.3192 -1.3499 0.7172 -2.9443 -0.1649
0.8622 -0.2050 -0.3034 -1.7115 0.3426 1.4172 -1.1471 0.3129 3.0349 1.6302 1.4384 0.6277
The command reshape does not generate this output. I do not want to do something like B=[A(1:4,:) A(5:8,:) A(9:end,:)], since the matrix I am having is much larger than the example. Is there a generalized way to do it?
  3 commentaires
KALYAN ACHARJYA
KALYAN ACHARJYA le 2 Juil 2019
@Stephen Exactly.
gsourop
gsourop le 2 Juil 2019
Modifié(e) : gsourop le 2 Juil 2019
@Stephen My apologies, you are right. I changed the example and copied wrong values. Please let's consider the following matrices:
rng default
A = randn(12,4)
A =
0.5377 0.7254 0.4889 0.3252
1.8339 -0.0631 1.0347 -0.7549
-2.2588 0.7147 0.7269 1.3703
0.8622 -0.2050 -0.3034 -1.7115
0.3188 -0.1241 0.2939 -0.1022
-1.3077 1.4897 -0.7873 -0.2414
-0.4336 1.4090 0.8884 0.3192
0.3426 1.4172 -1.1471 0.3129
3.5784 0.6715 -1.0689 -0.8649
2.7694 -1.2075 -0.8095 -0.0301
-1.3499 0.7172 -2.9443 -0.1649
3.0349 1.6302 1.4384 0.6277
B =
0.5377 0.7254 0.4889 0.3252 0.3188 -0.1241 0.2939 -0.1022 3.5784 0.6715 -1.0689 -0.8649
1.8339 -0.0631 1.0347 -0.7549 -1.3077 1.4897 -0.7873 -0.2414 2.7694 -1.2075 -0.8095 -0.0301
-2.2588 0.7147 0.7269 1.3703 -0.4336 1.4090 0.8884 0.3192 -1.3499 0.7172 -2.9443 -0.1649
0.8622 -0.2050 -0.3034 -1.7115 0.3426 1.4172 -1.1471 0.3129 3.0349 1.6302 1.4384 0.6277

Connectez-vous pour commenter.

Réponse acceptée

Andrei Bobrov
Andrei Bobrov le 2 Juil 2019
Modifié(e) : Andrei Bobrov le 2 Juil 2019
A =[...
0.5377 0.7254 0.4889 0.3252
1.8339 -0.0631 1.0347 -0.7549
-2.2588 0.7147 0.7269 1.3703
0.8622 -0.205 -0.3034 -1.7115
0.3188 -0.1241 0.2939 -0.1022
-1.3077 1.4897 -0.7873 -0.2414
-0.4336 1.409 0.8884 0.3192
0.3426 1.4172 -1.1471 0.3129
3.5784 0.6715 -1.0689 -0.8649
2.7694 -1.2075 -0.8095 -0.0301
-1.3499 0.7172 -2.9443 -0.1649
3.0349 1.6302 1.4384 0.6277];
[m,n] = size(A);
q = 4;
out = reshape(permute(reshape(A.',n,q,[]),[2,1,3]),q,[]);
  1 commentaire
KALYAN ACHARJYA
KALYAN ACHARJYA le 2 Juil 2019
@Andrei sir you always rocks +1

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by