Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

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.

    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.

Examples

collapse all

Create a five-element vector.

vec = [1,2,3,4,5];

Convert the vector to matrices with two, three, and four columns.

twoColumnMatrix = vec2mat(vec,2)
twoColumnMatrix = 

     1     2
     3     4
     5     0

threeColumnMatrix = vec2mat(vec,3)
threeColumnMatrix = 

     1     2     3
     4     5     0

fourColumnMatrix = vec2mat(vec,4)
fourColumnMatrix = 

     1     2     3     4
     5     0     0     0

Create a five-element vector.

vec = 1:5;

Specify a padding value for the matrix output by vec2Mat. Convert the vector to a four-column matrix using the nonzero padding value.

paddingValue = NaN;
mat = vec2mat(vec,4,paddingValue)
mat = 

     1     2     3     4
     5   NaN   NaN   NaN

You can also specify the padding value as a vector or matrix. vec2mat pads the output matrix with values taken from paddingValue in order.

paddingValue = [10,8,6;9,7,5]
paddingValue = 

    10     8     6
     9     7     5

mat2 = vec2mat(vec,3,paddingValue)
mat2 = 

     1     2     3
     4     5    10

mat3 = vec2mat(vec,4,paddingValue)
mat3 = 

     1     2     3     4
     5    10     9     8

You can optionally return the number of elements padded by vec2mat.

Create a five-element vector and a matrix of padding values.

vec = [1;2;3;4;5];
padding = [2,4;6,4];

Convert vec to matrices with two, three, and four columns. Specify nonzero padding and return the number padded in each scenario.

[mat2,numPadded2] = vec2mat(vec,2,padding)
mat2 = 

     1     2
     3     4
     5     2

numPadded2 = 1
[mat3,numPadded3] = vec2mat(vec,3,padding)
mat3 = 

     1     2     3
     4     5     2

numPadded3 = 1
[mat4,numPadded4] = vec2mat(vec,4,padding)
mat4 = 

     1     2     3     4
     5     2     6     4

numPadded4 = 3

See Also

Introduced before R2006a

Was this topic helpful?