Skip to Main Content Skip to Search
Product Documentation

vec2mat - Convert vector into matrix

Syntax

mat = vec2mat(vec,matcol)
mat = vec2mat(vec,matcol,padding)
[mat,padded] = vec2mat(...)

Description

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.

Examples

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

See Also

reshape

  


Free Early Verification Kit

Learn how to apply early verification to your development process through these technical resources.

How much time do you spend on testing to ensure implementation meets system-level requirements?

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS