Convert vector into matrix
mat = vec2mat(vec,matcol)
mat = vec2mat(vec,matcol,padding)
[mat,padded] = vec2mat(...)
mat = vec2mat(vec,matcol) converts the vector vec into a matrix with matcol columns, creating one row at a time. If the length of vec is not a multiple of matcol, then extra zeros are placed in the last row of mat. The matrix mat has ceil(length(vec)/matcol) rows.
mat = vec2mat(vec,matcol,padding) is the same as the first syntax, except that the extra entries placed in the last row of mat are not necessarily zeros. The extra entries are taken from the matrix padding, in order. If padding has fewer entries than are needed, then the last entry is used repeatedly.
[mat,padded] = vec2mat(...) returns an integer padded that indicates how many extra entries were placed in the last row of mat.
Note: vec2mat is similar to the built-in MATLAB® function reshape. However, given a vector input, reshape creates a matrix one column at a time instead of one row at a time. Also, reshape requires the input and output matrices to have the same number of entries, whereas vec2mat places extra entries in the output matrix if necessary.
vec = [1 2 3 4 5]; [mat,padded] = vec2mat(vec,3) [mat2,padded2] = vec2mat(vec,4) mat3 = vec2mat(vec,4,[10 9 8; 7 6 5; 4 3 2])
The output is below.
mat = 1 2 3 4 5 0 padded = 1 mat2 = 1 2 3 4 5 0 0 0 padded2 = 3 mat3 = 1 2 3 4 5 10 7 4