EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Matlab Tutorial Matlab Backslash
 

Matlab Backslash

Updated March 6, 2023

Matlab Backslash

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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:

Input:

example #1

Output:

matlab 1

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:

example #2

Output:

matlab 2

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:

matlab 5

Output:

matlab 3

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:

input

Output:

matlab 4

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 –

  1. Nyquist Matlab
  2. Matlab limit
  3. Impulse Response Matlab
  4. Matlab Saveas

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - ENROLL NOW