I have a 1260 by 1 column vector (myVector) that I want to reshape to a 35*36 matrix. However, I can't figure out how to reshape it the particular way that I want:
reshape(myVector,35,36) takes each successive chunk of 35 elements from myVector and makes them the 36 columns of the new matrix. But I want to take each successive chunk of 36 elements from myVector and make each chunk the 35 rows of the new matrix. How do I do this?

 Akzeptierte Antwort

Max Alger-Meyer
Max Alger-Meyer am 8 Mär. 2022

0 Stimmen

If I understand you correctly, all you need to do is transpose the reshaped result:
vector = 1:16
vector = 1×16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
array1 = reshape(vector,4,4)
array1 = 4×4
1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16
transpose(array1)
ans = 4×4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

5 Kommentare

Adam Fitchett
Adam Fitchett am 8 Mär. 2022
This solution only works if the desired matrix is square
However, I figured out a solution for non-square matrices: reshape first with the desired dimensions swapped and then transpose
So to get the desired 35*36 matrix, first do reshape(myVector,36,35), and then ans = ans'
James Tursa
James Tursa am 8 Mär. 2022
Bearbeitet: James Tursa am 8 Mär. 2022
Note that you can transpose the result of reshape directly, e.g.,
myMatrix = reshape(myVector,36,35)';
If elements could be complex then use the .' operator instead of the ' operator:
myMatrix = reshape(myVector,36,35).';
Amr Aboughazala
Amr Aboughazala am 29 Aug. 2022
I don't see this is a right answer if you have a matrix of different size.
x = [0,1,2,3,4,5]
it should be like if row wise x = reshape(x,3,2)
x =
0 1
2 3
4 5
if it is column wise
x =
0 3
1 4
2 5
The second is the answer when you use reshape(x,3,2)
taking the transpose won't give the first answer
@Amr Aboughazala "taking the transpose won't give the first answer"
because you take transpose on a wrong reshape.
The correct one is
x = [0,1,2,3,4,5];
reshape(x,[2,3])' % size is [2,3] not [3,2]
ans = 3×2
0 1 2 3 4 5
It does fine.
Amr Aboughazala
Amr Aboughazala am 29 Aug. 2022
I just noticed that, thank you so much for your reply :)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Produkte

Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by