If I have a string e.g.
A= 1 2 3 4 5 6 7 8 9
How can I change that into: [1 2 3;4 5 6;7 8 9]
1 2 3
4 5 6
7 8 9
If I use the reshape command e.g. reshape(A,3,[]) it will turn it into
1 4 7
2 5 8
3 6 9
But I need it to be like: [1 2 3;4 5 6;7 8 9]

 Réponse acceptée

Transpose after doing the reshape call:
A = [1 2 3 4 5 6 7 8 9];
Ar = reshape(A,3,[]).'
Ar = 3×3
1 2 3 4 5 6 7 8 9
.

2 commentaires

Roger Yang
Roger Yang le 24 Avr 2021
Modifié(e) : Roger Yang le 24 Avr 2021
Thank you so much my example isn't very good because it still doesn't really solve my problem. The rows and colums swapped but I don't want it to transpose e.g.
A=[1,2,3,4,5,6,7,8,9,10]
if I used
reshape(A,2,[]).'
that will give me
1 2
3 4
5 6
7 8
9 10
but I want it to be
1 2 3 4 5
6 7 8 9 10
How can I achieve that
My pleasure!
The reshape function can’t do everything, however it can produce that result. It’s necessary to experiment with it to get the result you want.
Here —
A = [1 2 3 4 5 6 7 8 9 10];
Ar = reshape(A, [], 2).'
Ar = 2×5
1 2 3 4 5 6 7 8 9 10
As desired.
It’s certainly possible to want a result it can’t produce, even with transposition or with permute (essentially multi-dimensional dimension-shuffling), so it definitely has its limitations.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by