## Matlab Backslash Operator

MATLAB backslash operator is used to solving a linear equation of the form a*x = b, where ‘a’ and ‘b’ are matrices and ‘x’ is a vector. The solution of this equation is given by x = a \ b, but it works only if the number of rows in ‘a’ and ‘b’ is equal. If the number of rows is not equal, and ‘a’ is not a scalar, we will get a warning from MATLAB.

**Syntax of the backslash operator:**

`x = a \ b`

Details of the backslash operator:

1. x = a \ b will perform the left division on the matrix also called backslash

2. If ‘a’ is a scalar, then the backslash operator will perform an element-wise division

### How to use the backslash operator in MATLAB?

Let us now understand how to use the backslash operator in MATLAB.

#### Example #1

In this example, we will use the backslash operator on a 3 x 3 matrix. We will initialize two matrices, one a 3 x 3 matrix, and the other a 3 x 1 matrix to create and solve the linear equation a*x = b

**Code:**

```
a = [4 6 12; 1 10 6; 14 4 3]
[Initializing a 3 x 3 matrix as the first input]
b = [2; 4; 7]
[Initializing a 3 x 1 matrix as the second input]
x = a \ b
```

[Using the back slash operator to solve the linear equation formed by ‘a’ and ‘b’]

This is how our input and output will look like in MATLAB:

4.5 (7,739 ratings)

View Course

**Input:**

**Output:**

As we can see, we have obtained the solution for the equation a*x = b as the output by using the back slash operator

#### Example #2

In this example, we will use the backslash operator on a 4 x 4 matrix. Here also, we will initialize 2 matrices, one a 4 x 4 matrix, and the other a 4 x 1 matrix to create and solve the linear equation a*x = b

**Code:**

```
a = [6 1 12 2; 1 1 4 5; 4 12 3 11; 2 4 6 1]
[Initializing a 4 x 4 matrix as the first input]
b = [3; 4; 8; 3]
[Initializing a 4 x 1 matrix as the second input]
x = a \ b
```

[Using the backslash operator to solve the linear equation formed by ‘a’ and ‘b’]

This is how our input and output will look like in MATLAB:

**Input:**

**Output:**

As we can see, we have obtained the solution for the equation a*x = b as the output by using the backslash operator.

#### Example #3

In this example, we will use the backslash operator on a sparse matrix. Please note that a sparse matrix is a matrix with only a small number of non-zero elements. Here we will initialize two sparse matrices to create and solve the linear equation a*x = b.

**Code:**

`a = sparse ([0 0 1 3 0; 0 1 4 0 0; 1 0 0 0 4; 2 1 0 0 0; 1 0 4 0 0])`

[Initializing a 5 x 5 sparse matrix as the first input]
b = sparse ([4; -1; -8; 6; 10])

[Initializing a 5 x 1 sparse matrix as the second input]
x = a \ b

[Using the backslash operator to solve the linear equation formed by ‘a’ and ‘b’]

This is how our input and output will look like in MATLAB:

**Input:**

**Output:**

As we can see, we have obtained the solution for the equation a*x = b as the output by using the backslash operator on sparse matrices.

In the above examples, we have taken the number of rows in both the matrices as equal. Next, let us see what will happen if the number of rows in the input matrices is not equal.

#### Example #4

In this example, we will use the backslash operator on a 3 x 3 matrix and a 2 x 1 matrix. Please note that we are taking two matrices with a different number of rows.

**Code:**

```
a = [1 6 22; 1 0 8; 4 14 3]
[Initializing a 3 x 3 matrix as the first input]
b = [2; 4]
[Initializing a 2 x 1 matrix as the second input]
x = a \ b
```

[Using the backslash operator to solve the linear equation formed by ‘a’ and ‘b’]

This is how our input and output will look like in MATLAB:

**Input:**

**Output:**

As we can see, we have obtained an error message in the output when the number of rows in the input matrices is not equal.

### Conclusion

- The backslash operator is used to solve a linear equation of the form a*x = b, where ‘a’ and ‘b’ are matrices and ‘x’ is a vector.
- It is used to calculate the left division between two matrices.
- For backslash operator to work, both the input matrices must have an equal number of rows.

### Recommended Articles

This is a guide to Matlab Backslash. Here we discuss the Introduction, syntax, How to use the backslash operator in MATLAB? examples with code implementation. You may also have a look at the following articles to learn more –