mtimes, *

Symbolic matrix multiplication

Syntax

``A*B``
``mtimes(A,B)``

Description

example

````A*B` is the matrix product of `A` and `B`. If `A` is an `m`-by-`p` and `B` is a `p`-by-`n` matrix, then the result is an `m`-by-`n` matrix `C` defined as$C\left(i,j\right)=\sum _{k=1}^{p}A\left(i,k\right)B\left(k,j\right)$For nonscalar `A` and `B`, the number of columns of `A` must equal the number of rows of `B`. Matrix multiplication is not universally commutative for nonscalar inputs. That is, typically `A*B` is not equal to `B*A`. If at least one input is scalar, then `A*B` is equivalent to `A.*B` and is commutative.```
````mtimes(A,B)` is equivalent to `A*B`.```

Examples

Multiply Two Vectors

Create a `1`-by-`5` row vector and a `5`-by-`1` column vector.

```syms x A = [x, 2*x^2, 3*x^3, 4*x^4] B = [1/x; 2/x^2; 3/x^3; 4/x^4]```
```A = [ x, 2*x^2, 3*x^3, 4*x^4] B = 1/x 2/x^2 3/x^3 4/x^4```

Find the matrix product of these two vectors.

`A*B`
```ans = 30```

Multiply Two Matrices

Create a `4`-by-`3` matrix and a `3`-by-`2` matrix.

```A = sym('a%d%d', [4 3]) B = sym('b%d%d', [3 2])```
```A = [ a11, a12, a13] [ a21, a22, a23] [ a31, a32, a33] [ a41, a42, a43] B = [ b11, b12] [ b21, b22] [ b31, b32]```

Multiply `A` by `B`.

`A*B`
```ans = [ a11*b11 + a12*b21 + a13*b31, a11*b12 + a12*b22 + a13*b32] [ a21*b11 + a22*b21 + a23*b31, a21*b12 + a22*b22 + a23*b32] [ a31*b11 + a32*b21 + a33*b31, a31*b12 + a32*b22 + a33*b32] [ a41*b11 + a42*b21 + a43*b31, a41*b12 + a42*b22 + a43*b32]```

Multiply Matrix by Scalar

Create a `4`-by-`4` Hilbert matrix `H`.

`H = sym(hilb(4))`
```H = [ 1, 1/2, 1/3, 1/4] [ 1/2, 1/3, 1/4, 1/5] [ 1/3, 1/4, 1/5, 1/6] [ 1/4, 1/5, 1/6, 1/7]```

Multiply `H` by `eπ`.

`C = H*exp(sym(pi))`
```C = [ exp(pi), exp(pi)/2, exp(pi)/3, exp(pi)/4] [ exp(pi)/2, exp(pi)/3, exp(pi)/4, exp(pi)/5] [ exp(pi)/3, exp(pi)/4, exp(pi)/5, exp(pi)/6] [ exp(pi)/4, exp(pi)/5, exp(pi)/6, exp(pi)/7]```

Use `vpa` and `digits` to approximate symbolic results with the required number of digits. For example, approximate it with five-digit accuracy.

```old = digits(5); vpa(C) digits(old)```
```ans = [ 23.141, 11.57, 7.7136, 5.7852] [ 11.57, 7.7136, 5.7852, 4.6281] [ 7.7136, 5.7852, 4.6281, 3.8568] [ 5.7852, 4.6281, 3.8568, 3.3058]```

Input Arguments

collapse all

Input, specified as a symbolic number, variable, expression, function, vector, or matrix. Inputs `A` and `B` must be the same size unless one is a scalar. A scalar value expands into an array of the same size as the other input.

Input, specified as a symbolic number, variable, expression, function, vector, or matrix. Inputs `A` and `B` must be the same size unless one is a scalar. A scalar value expands into an array of the same size as the other input.

Mathematical Modeling with Symbolic Math Toolbox

Get examples and videos