How to reshapre a matrix?
1 view (last 30 days)
Show older comments
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 Comments
Accepted Answer
Andrei Bobrov
on 2 Jul 2019
Edited: Andrei Bobrov
on 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,[]);
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!