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]

 채택된 답변

Star Strider
Star Strider 2021년 4월 24일
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

Roger Yang
Roger Yang 2021년 4월 24일
편집: Roger Yang 2021년 4월 24일
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.

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

추가 답변 (0개)

카테고리

질문:

2021년 4월 23일

댓글:

2021년 4월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by