## Create Symbolic Matrices

### Use Existing Symbolic Variables

A circulant matrix has the property that each row is obtained from the previous one by cyclically permuting the entries one step forward. For example, create the symbolic circulant matrix whose elements are `a`, `b`, and `c`, using the commands:

```syms a b c A = [a b c; c a b; b c a]```
```A = [ a, b, c] [ c, a, b] [ b, c, a]```

Since matrix `A` is circulant, the sum of elements over each row and each column is the same. Find the sum of all the elements of the first row:

`sum(A(1,:))`
```ans = a + b + c```

To check if the sum of the elements of the first row equals the sum of the elements of the second column, use the `isAlways` function:

`isAlways(sum(A(1,:)) == sum(A(:,2)))`

The sums are equal:

```ans = logical 1```

From this example, you can see that using symbolic objects is very similar to using regular MATLAB® numeric objects.

### Generate Elements While Creating a Matrix

The `sym` function also lets you define a symbolic matrix or vector without having to define its elements in advance. In this case, the `sym` function generates the elements of a symbolic matrix at the same time that it creates a matrix. The function presents all generated elements using the same form: the base (which must be a valid variable name), a row index, and a column index. Use the first argument of `sym` to specify the base for the names of generated elements. You can use any valid variable name as a base. To check whether the name is a valid variable name, use the `isvarname` function. By default, `sym` separates a row index and a column index by underscore. For example, create the 2-by-4 matrix `A` with the elements ```A1_1, ..., A2_4```:

`A = sym('A', [2 4])`
```A = [ A1_1, A1_2, A1_3, A1_4] [ A2_1, A2_2, A2_3, A2_4]```

To control the format of the generated names of matrix elements, use `%d` in the first argument:

`A = sym('A%d%d', [2 4])`
```A = [ A11, A12, A13, A14] [ A21, A22, A23, A24]```

### Create Matrix of Symbolic Numbers

A particularly effective use of `sym` is to convert a matrix from numeric to symbolic form. The command

`A = hilb(3)`

generates the 3-by-3 Hilbert matrix:

```A = 1.0000 0.5000 0.3333 0.5000 0.3333 0.2500 0.3333 0.2500 0.2000```

By applying `sym` to `A`

`A = sym(A)`

you can obtain the precise symbolic form of the 3-by-3 Hilbert matrix:

```A = [ 1, 1/2, 1/3] [ 1/2, 1/3, 1/4] [ 1/3, 1/4, 1/5]```

For more information on numeric to symbolic conversions, see Numeric to Symbolic Conversion.

## Support

#### Mathematical Modeling with Symbolic Math Toolbox

Get examples and videos