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]

 Respuesta aceptada

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 comentarios

Roger Yang
Roger Yang el 24 de Abr. de 2021
Editada: Roger Yang el 24 de Abr. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Preguntada:

el 23 de Abr. de 2021

Comentada:

el 24 de Abr. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by