# How to reshapre a matrix?

조회 수: 1 (최근 30일)
gsourop 2019년 7월 2일
댓글: KALYAN ACHARJYA 2019년 7월 2일
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이전 댓글 1개 표시이전 댓글 1개 숨기기
KALYAN ACHARJYA 2019년 7월 2일
@Stephen Exactly.
gsourop 2019년 7월 2일
편집: gsourop 2019년 7월 2일
@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

댓글을 달려면 로그인하십시오.

### 채택된 답변

Andrei Bobrov 2019년 7월 2일
편집: Andrei Bobrov 2019년 7월 2일
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이전 댓글 -1개 표시이전 댓글 -1개 숨기기
KALYAN ACHARJYA 2019년 7월 2일
@Andrei sir you always rocks +1

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by