# Matlab-Matrix - Multiplication

#### Data Preprocessing for Machine Learning using MATLAB

30 Lectures 4 hours

#### Image Processing Toolbox in MATLAB

17 Lectures 3 hours

#### Matlab - The Complete Course

Best Seller

37 Lectures 5 hours

Consider two matrices A and B. If A is an m x n matrix and B is an n x p matrix, they could be multiplied together to produce an m x n matrix C. Matrix multiplication is possible only if the number of columns n in A is equal to the number of rows n in B.

In matrix multiplication, the elements of the rows in the first matrix are multiplied with the corresponding columns in the second matrix.

Each element in the (i, j)thposition, in the resulting matrix C, is the summation of the products of elements in ith row of the first matrix with the corresponding element in the jth column of the second matrix.

Matrix multiplication in MATLAB is performed by using the * operator.

### Example

Consider following example in MATLAB

```a = [ 1 2 3; 2 3 4; 1 2 5];
b = [ 2 1 3 ; 5 0 -2; 2 3 -1];
prod = a * b
```

### Output

The execution in MATLAB will display the following result −

```>> a = [ 1 2 3; 2 3 4; 1 2 5];
b = [ 2 1 3 ; 5 0 -2; 2 3 -1];
prod = a * b

prod =

18  10  -4
27  14  -4
22  16  -6

>>
```

## The mtimes function

You can also make use of the function mtimes to multiply two given matrices. It is a builtin function available in MATLAB.

### Example

Consider following example −

```a = [ 1 2 3; 2 3 4; 1 2 5];
b = [ 2 1 3 ; 5 0 -2; 2 3 -1];
test= mtimes(a,b)
```

### Output

On execution in MATLAB the output is as follows −

```>> a = [ 1 2 3; 2 3 4; 1 2 5];
b = [ 2 1 3 ; 5 0 -2; 2 3 -1];
test= mtimes(a,b)

test =

18  10  -4
27  14  -4
22  16  -6

>>
```