MATLAB: Problems converting a row(1, length) to a column matrix (3×3)

arrayfor loopMATLABmatricesmatrix arraymatrix manipulation

Hello Matlab Community, I'm trying to make a function that would require a "reshaping" of a matrix of 1 row, n columns (columns = number of values read e.g 258 values, 258 column) to a matrix of nx3. (N rows, 3 fixed columns)
Since the number of values (N) read may fluctuate and may not be a multiple of 3, I can't use the reshape() matrix tool.
Is it possible that I can store the long serial value row into a nx3 matrix?
My approach was using a for loop to strategically putting the first 3 values into the first row, followed by the next 3 till the last value e.g 258 values.
Seen below e.g.
where a = (10, 1)
1,2,3,4,5,6,7,8,9,10 ... N-1 (say e.g 30)
to a Nx3 matrix:
a =
1 2 3
4 5 6
7 8 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
Thank you.

Best Answer

I am not certain what you want to do.
Try this:
a = 1:8; % Original Vector
N = 8; % Rows In Matrix
a = [a zeros(1, N*3-numel(a))]; % Extend Vector To Be Compatible With ‘reshape’
b = reshape(a, 3, [])' % Reshape & Transpose
b =
1 2 3
4 5 6
7 8 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0