Filter löschen
Filter löschen

How to reshapre a matrix?

1 Ansicht (letzte 30 Tage)
gsourop
gsourop am 2 Jul. 2019
Kommentiert: KALYAN ACHARJYA am 2 Jul. 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 Kommentare
KALYAN ACHARJYA
KALYAN ACHARJYA am 2 Jul. 2019
@Stephen Exactly.
gsourop
gsourop am 2 Jul. 2019
Bearbeitet: gsourop am 2 Jul. 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

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 2 Jul. 2019
Bearbeitet: Andrei Bobrov am 2 Jul. 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 Kommentar
KALYAN ACHARJYA
KALYAN ACHARJYA am 2 Jul. 2019
@Andrei sir you always rocks +1

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by