Documentation

# vec2mat

Convert vector to matrix

## Syntax

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

## Description

example

````mat = vec2mat(vec,matcol)` converts vector `vec` to matrix `mat` with `matcol` columns. The function creates the matrix one row at a time, filling the rows with elements from `vec` in order. If the length of `vec` is not a multiple of `matcol`, then the function pads the last row of `mat` with zeros until the row contains `matcol` elements.```

example

````mat = vec2mat(vec,matcol,padding)` specifies values for the function to use to pad the last row of `mat`. The function uses the value from `padding` in order.```

example

````[mat,padded] = vec2mat(___)` also returns `padded`, the number of padded elements in the last row of `mat`. You can specify any of the input argument combinations from previous syntaxes. Note`vec2mat` is similar to the MATLAB® function `reshape`. However, given a vector input, `reshape` creates its corresponding matrix one column at a time (instead of one row at a time). Also, `reshape` requires its input and output arrays to have the same number of elements, whereas `vec2mat` pads its output matrix if necessary. ```

## Examples

collapse all

Create a five-element numeric vector.

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

Convert the vector to two-column matrix. Repeat the conversion for a three-column and a four-column matrix respectively. Display each resulting matrix.

`twoColumnMatrix = vec2mat(vec,2)`
```twoColumnMatrix = 3×2 1 2 3 4 5 0 ```
`threeColumnMatrix = vec2mat(vec,3)`
```threeColumnMatrix = 2×3 1 2 3 4 5 0 ```
`fourColumnMatrix = vec2mat(vec,4)`
```fourColumnMatrix = 2×4 1 2 3 4 5 0 0 0 ```

Create a five-element numeric vector.

`vec = 1:5;`

Specify a nonzero padding value. Convert the vector to a four-column matrix, specifying the nonzero padding value.

```paddingValue = NaN; mat = vec2mat(vec,4,paddingValue)```
```mat = 2×4 1 2 3 4 5 NaN NaN NaN ```

`paddingValue = [10,8,6;9,7,5]`
```paddingValue = 2×3 10 8 6 9 7 5 ```

Convert the vector to a three-column and a four-column matrix respectively, specifying the nonzero padding values.

`mat2 = vec2mat(vec,3,paddingValue)`
```mat2 = 2×3 1 2 3 4 5 10 ```
`mat3 = vec2mat(vec,4,paddingValue)`
```mat3 = 2×4 1 2 3 4 5 10 9 8 ```

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

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

Convert the vector to a two-column matrix, specifying the padding values and returning the number of padded elements. Repeat the conversion for a three-column and a four-column matrix, respectively. Display each resulting matrix.

`[mat2,numPadded2] = vec2mat(vec,2,padding)`
```mat2 = 3×2 1 2 3 4 5 2 ```
```numPadded2 = 1 ```
`[mat3,numPadded3] = vec2mat(vec,3,padding)`
```mat3 = 2×3 1 2 3 4 5 2 ```
```numPadded3 = 1 ```
`[mat4,numPadded4] = vec2mat(vec,4,padding)`
```mat4 = 2×4 1 2 3 4 5 2 6 4 ```
```numPadded4 = 3 ```

## Input Arguments

collapse all

Input array, specified as a vector.

Data Types: `single` | `double` | `int8` | `int16` | `int32` | `int64` | `uint8` | `uint16` | `uint32` | `uint64` | `logical`
Complex Number Support: Yes

Number of columns for the output matrix `mat`, specified as a positive integer. If the length of `vec` is not a multiple of `matcol`, then the function pads the last row of `mat` with zeros until the row contains `matcol` elements.

Data Types: `double`

Padding values for the last row of `mat`, specified as a vector or matrix. The `padding` input inherits the data type of the `vec` input. The function uses the values from `padding` in order. If `padding` has fewer elements than what the function needs to complete the last row of `mat`, then the function repeats the last element of `padding` until `mat` is full.

## Output Arguments

collapse all

Output array, returned as a matrix with elements from `vec` and having `matcol` columns. The output inherits the data type of the input. The number of rows is equal to `ceil(length(vec)/matcol)`.

Number of padded elements in the last row of `mat`, returned as a positive integer.