How can I reshape a matrix in cases where the reshape function might not work properly?

4 views (last 30 days)
I have a 4x5 array of zeroes which I would like to populate using elements from this 1 x 20 vector.:
X = [19 14 18 4 17 21 4 0 13 3 19 14 15 17 14 19 4 2 19 88]
The first 5 elements of X must go in the first row of the array A, and the next 5 elements of X must go into the 2nd row, and so on...
I've tried setting up 2 for loops, but I'm stuck with the index:
for i = 1:4
for a = 1:5
A(i,a) = X(%??)
end
end
I'm not sure how to index X such that, for example, A(2,1) = X(1,6)
As another attempt to fix this, I was able to reshape X into a 5 x 4 array which satisfied my conditions (the first 4 elements of X are in row 1, the next 4 are in row 2, etc.) however, it is a 5x4 array and not a 4x5 array:
Y = 19 14 18 4
17 21 4 0
13 3 19 14
15 17 14 19
4 2 19 88
Attempting to use reshape to turn it into a 4x5 array does not work since it doesn't give me the correct output, and neither does transposing the array. I'd appreciate some help with this. Thanks!

Accepted Answer

Walter Roberson
Walter Roberson on 30 Nov 2018
A = reshape(X, 5, 4).' ;

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!