How to reshape a matrix (something that can't be done with reshape function)

1 vue (au cours des 30 derniers jours)
Let's say I have a matrix A:
A = [1 4 7 10; 2 5 8 11; 3 6 9 12]
A =
1 4 7 10
2 5 8 11
3 6 9 12
and B = reshape(A,6,2) will return:
B =
1 7
2 8
3 9
4 10
5 11
6 12
But I'm wondering if there is any way to rearrange this matrix into something that looks like this:
B =
1 4
2 5
3 6
7 10
8 11
9 12
I'd like to know an easier way than doing:
B = [A(:,[1 2]) ; A(:,[3 4])]
Thank you in advance!

Réponse acceptée

Stephen23
Stephen23 le 12 Mai 2021
Modifié(e) : Stephen23 le 12 Mai 2021
A = [1,4,7,10;2,5,8,11;3,6,9,12]
A = 3×4
1 4 7 10 2 5 8 11 3 6 9 12
nmc = 2; % output number of columns
nmr = 3; % output number of rows per "group"
B = reshape(permute(reshape(A,nmr,nmc,[]),[1,3,2]),[],nmc)
B = 6×2
1 4 2 5 3 6 7 10 8 11 9 12
  2 commentaires
Walter Roberson
Walter Roberson le 12 Mai 2021
Which is to say that it can be done but it is by no means "easier" than extracting the two halves.
Kenta Watanabe
Kenta Watanabe le 12 Mai 2021
Thanks a lot! I didn't expect to get an answer this fast.
But I also understood that there may not be an easy way to do this. Thanks though!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by