MATLAB: How to “squeeze” / transfer variables from a row vector into a NxN matrix

for loopsindexingnested for loopsnested loopsrow-vectorstransposing

Hi.
I have a 1×21 row vector matrix:
A = linspace(-2,0,21)
That I want to transfer the values over to a 3×7 matrix (3×7 because it will fit the 21 points)
I have this so far:
B = zeros(3,7) % Creates 3x7 matrix of zeros
m = length(B);
Though not sure how to implement a for loop to transfer over the values (possibly through indexing) from the row vector to each row and column entry:
Goal is to have a 3×7 matrix like this:
-2 -1.7 -1.4 -1.1 -0.8 -0.5 -0.2
-1.9 -1.6 -1.3 -1.0 -0.7 -0.4 -0.1
-1.8 -1.5 -1.2 -0.9 -0.6 -0.3 -0
So somehow I need to have each column entry in the row vector transfer to each row and column entry starting from R1C1, R2C1, R3C1, R1C2……R3C7 (R being row and C being column)
Where R1C1 = -2, R2C1 = -1.9,……R3C7 = 0
I attempted a basic (very bare basic) for loop though I don't know how to progress. I am assuming nested for loops and if statements and indexing to find the proper row and column entries.
for i = 1:length(D)
for j = 1:length(D)
if
end
end
end
Not sure if I should be using the value of length(D) since that value is 7 (7 columns) instead of the length of the rows i.e. 3 rows, since after the 3rd entry of each row is transferred, I want the 1st entry of the next column to be transferred to?
Maybe the row vector can be split up into pieces i.e. 7 pieces (seven 3×1 column vectors) that can somehow be split, transposed (row to column vectors) and transferred over to the 3×7 matrix?
Any suggestions? Thanks.

Best Answer

Since MATLAB stores 2D matrix values in column order, a simple reshape will do the job:
B = reshape(A,3,7);
If you wanted the results to be ordered by rows instead of columns, then an additional transpose would be required. E.g.,
B = reshape(A,7,3)'; % <-- Start with 7x3 and then transpose it