# mtimes, *

Matrix Multiplication

## Syntax

• `C = A*B` example
• `C = mtimes(A,B)`

## Description

example

````C = 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 `C` is an m-by-n matrix defined by $C\left(i,j\right)=\sum _{k=1}^{p}A\left(i,k\right)B\left(k,j\right).$This definition says that `C(i,j)` is the inner product of the `i`th row of `A` with the `j`th column of `B`. You can write this definition using the MATLAB® colon operator as C(i,j) = A(i,:)*B(:,j)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, `A*B` is typically not equal to `B*A`. If at least one input is scalar, then `A*B` is equivalent to `A.*B` and is commutative.```
````C = mtimes(A,B)` is an alternative way to execute `A*B`, but is rarely used. It enables operator overloading for classes.```

## Examples

collapse all

### Multiply Two Vectors

Create a 1-by-4 row vector, `A`, and a 4-by-1 column vector, `B`.

```A = [1 1 0 0]; B = [1; 2; 3; 4];```

Multiply `A` times `B`.

`C = A*B`
```C = 3```

The result is a 1-by-1 scalar, also called the dot product or inner product of the vectors `A` and `B`. Alternatively, you can calculate the dot product A • B with the syntax `dot(A,B)`.

Multiply `B` times `A`.

`C = B*A`
```C = 1 1 0 0 2 2 0 0 3 3 0 0 4 4 0 0```

The result is a 4-by-4 matrix, also called the outer product of the vectors `A` and `B`. The outer product of two vectors, A ⊗ B, returns a matrix.

### Multiply Two Arrays

Create two arrays, `A` and `B`.

```A = [1 3 5; 2 4 7]; B = [-5 8 11; 3 9 21; 4 0 8]; ```

Calculate the product of `A` and `B`.

```C = A*B ```
```C = 24 35 114 30 52 162```

Calculate the inner product of the second row of `A` and the third column of `B`.

```A(2,:)*B(:,3) ```
```ans = 162```

This answer is the same as `C(2,3)`.

## Input Arguments

collapse all

### `A` — Left Arrayscalar | vector | matrix

Left Array, specified as a scalar, vector, or matrix. For nonscalar inputs, the number of columns in `A` must be equal to the number of rows in `B`.

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

### `B` — Right Arrayscalar | vector | matrix

Right Array, specified as a scalar, vector, or matrix. For nonscalar inputs, the number of columns in `A` must be equal to the number of rows in `B`.

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

## Output Arguments

collapse all

### `C` — Product Arrayscalar | vector | matrix

Product Array, returned as a scalar, vector, or matrix. Array `C` has the same number of rows as input `A` and the same number of columns as input `B`. For example, if `A` is an m-by-0 empty matrix and `B` is a 0-by-n empty matrix, then `A*B` is an m-by-n matrix of zeros.