# symmatrix

Create symbolic matrix variable

## Syntax

``X = symmatrix('X',[nrow ncol])``
``X = symmatrix('X',n)``
``X = symmatrix('X')``
``X = symmatrix(S)``

## Description

example

````X = symmatrix('X',[nrow ncol])` creates an `nrow`-by-`ncol` symbolic matrix variable `X`. Symbolic matrix variables represent matrices, vectors, and scalars in compact matrix notation. For more information, see Create Symbolic Matrix Variables.```

example

````X = symmatrix('X',n)` creates an `n`-by-`n` symbolic matrix variable `X`.```
````X = symmatrix('X')` creates a 1-by-1 symbolic matrix variable `X`.```

example

````X = symmatrix(S)` converts a numeric matrix or a matrix of symbolic scalar variables specified by `S` to a symbolic matrix variable `X`.```

## Examples

collapse all

Create two symbolic matrix variables with size `2`-by-`3`. Nonscalar symbolic matrix variables are displayed as bold characters in the Live Editor and Command Window.

`A = symmatrix('A',[2 3])`
`A = $A$`
`B = symmatrix('B',[2 3])`
`B = $B$`

Add the two matrices. The summation of the two symbolic matrix variables is denoted by the matrix notation $\text{A}+\text{B}$.

`X = A + B`
`X = $A+B$`

Symbolic matrix variables represent matrices, vectors, and scalars in compact matrix notation. When representing nonscalars, these variables are noncommutative. When mathematical formulas involve matrices and vectors, writing them using symbolic matrix variables is more concise and clear than writing them componentwise.

Create two symbolic matrix variables.

```A = symmatrix('A',[2 2]); B = symmatrix('B',[2 2]);```

Check the commutation relation for multiplication between two symbolic matrix variables.

`A*B - B*A`
`ans = $A B-B A$`
`isequal(A*B,B*A)`
```ans = logical 0 ```

Check the commutation relation for addition between two symbolic matrix variables.

`isequal(A+B,B+A)`
```ans = logical 1 ```

Create `3`-by-`3` and `3`-by-`1` symbolic matrix variables.

`A = symmatrix('A',3)`
`A = $A$`
`X = symmatrix('X',[3 1])`
`X = $X$`

Find the Hessian matrix of ${\text{X}}^{T}\text{A}\text{X}$. Derived equations involving symbolic matrix variables are displayed in typeset as they would be in textbooks.

```f = X.'*A*X; H = diff(f,X,X.')```
`H = ${A}^{\mathrm{T}}+A$`

Create a Hilbert matrix of order 4. The data type of the matrix is `double`.

`H = hilb(4)`
```H = 4×4 1.0000 0.5000 0.3333 0.2500 0.5000 0.3333 0.2500 0.2000 0.3333 0.2500 0.2000 0.1667 0.2500 0.2000 0.1667 0.1429 ```
`class(H)`
```ans = 'double' ```

Convert the numeric matrix to a symbolic matrix variable. The data type of the converted matrix is `symmatrix`.

`X = symmatrix(H)`
```X =  $\left(\begin{array}{cccc}1& \frac{1}{2}& \frac{1}{3}& \frac{1}{4}\\ \frac{1}{2}& \frac{1}{3}& \frac{1}{4}& \frac{1}{5}\\ \frac{1}{3}& \frac{1}{4}& \frac{1}{5}& \frac{1}{6}\\ \frac{1}{4}& \frac{1}{5}& \frac{1}{6}& \frac{1}{7}\end{array}\right)$```
`class(X)`
```ans = 'symmatrix' ```

Create two symbolic matrix variables with size `2`-by-`2`.

`A = symmatrix('A',2)`
`A = $A$`
`B = symmatrix('B',2)`
`B = $B$`

Perform matrix multiplication between `A` and `B`. The multiplication of the two symbolic matrix variables is represented by the matrix notation $\text{A}\text{B}$.

`X = A*B`
`X = $A B$`

Convert the symbolic matrix variable `X` to a matrix of symbolic scalar variables `S`. The multiplication of two matrices of symbolic scalar variables is represented by the elements of the matrix product.

`S = symmatrix2sym(X)`
```S =  $\left(\begin{array}{cc}{A}_{1,1} {B}_{1,1}+{A}_{1,2} {B}_{2,1}& {A}_{1,1} {B}_{1,2}+{A}_{1,2} {B}_{2,2}\\ {A}_{2,1} {B}_{1,1}+{A}_{2,2} {B}_{2,1}& {A}_{2,1} {B}_{1,2}+{A}_{2,2} {B}_{2,2}\end{array}\right)$```

## Input Arguments

collapse all

Variable name, specified as a character vector. Argument `X` must be a valid variable name. That is, `X` must begin with a letter and can contain only alphanumeric characters and underscores. To verify that the name is a valid variable name, use `isvarname`.

Example: `x`, `y12`, `z_1`

Vector or matrix dimensions, specified as a vector of integers. `nrow` is the number of rows, and `ncol` is the number of columns. As a shortcut, you can create a square symbolic matrix variable by specifying only one integer. For example, `X = symmatrix('X',3)` creates a square `3`-by-`3` symbolic matrix variable.

Example: `[2 3]`, `[2,3]`

Numeric matrix or matrix of symbolic scalar variables to be converted to symbolic matrix variable, specified as a number, numeric matrix, symbolic scalar variable, or a matrix of symbolic scalar variables.

Example: `10`, `eye(3)`, `pi`, `hilb(3)`

## Limitations

• Using Symbolic Math Toolbox™, you can create symbolic functions that depend on symbolic scalar variables as parameters. However, symbolic matrix variables cannot be parameter-dependent. For example, the command `symmatrix('A(x)',[3 2])` currently errors.

• Differentiation functions, such as `jacobian` and `laplacian`, currently do not accept symbolic matrix variables as input. To evaluate differentiation with respect to vectors and matrices, you can use the `diff` function instead.

• To show all the functions in Symbolic Math Toolbox that accept symbolic matrix variables as input, use the command `methods symmatrix`.

## Alternative Functionality

### Alternative Approaches for Creating Symbolic Matrix Variables

To create several symbolic matrix variables in one function call, use ```syms var1 ... varN [nrow ncol] matrix```. For more details, see `syms`.

Introduced in R2021a

## Support

#### Mathematical Modeling with Symbolic Math Toolbox

Get examples and videos