MATLAB Answers


How does the backslash operator work when A is full?

I would like to know how the backslash operator works when A is full, and what the outline of the algorithm is.


2 Answers

Answer by MathWorks Support Team on 15 Jan 2019
 Accepted Answer

The method used by the backslash and forward slash operators to solve the system of linear equations A*x = b depends on the structure of the coefficient matrix A. In MATLAB 6.5 (R13), the types of structures the backslash operator tests the coefficient matrix for are given in the algorithm section of the following web page:
You can also access this webpage by typing "doc arithmeticoperators" (without the quotes) at the MATLAB prompt.
For earlier versions of MATLAB, the same testing for special structure was performed but fewer special structures were tested. Below is some pseudo-code describing how the backslash operator worked for MATLAB 6.0 (R12) and 6.1 (R12.1) in the case where A is full; you can find a full description in the documentation page for "arithmeticoperators".
>> x = A \ b;
% This is pseudo-code for how full \ works:
if size(A,1) == size(A,2) % A is square
if isequal(A,tril(A)) % A is lower triangular
x = A \ b; % This is a simple forward substitution on b
elseif isequal(A,triu(A)) % A is upper triangular
x = A \ b; % This is a simple backward substitution on b
if isequal(A,A') % A is symmetric
[R,p] = chol(A);
if (p == 0) % A is symmetric positive definite
x = R \ (R' \ b); % a forward and a backward substitution
[L,U,P] = lu(A); % general, square A
x = U \ (L \ (P*b)); % a forward and a backward substitution
else % A is rectangular
[Q,R] = qr(A);
x = R \ (Q' * b);
You can look at Golub and van Loan's Matrix Computations for more information on the various decompositions and pointers to the literature. Also see the LAPACK User's Guide for details of the LAPACK functions listed at the bottom of the documentation page on arithmeticoperators, such as DLANGE, DPOTRF, DPOTRS, DPOCON, DGESV, etc. You can find the LAPACK User's Guide at:


Sign in to comment.

Answer by Aiya Toongsooksai on 2 Jul 2019
Edited by Aiya Toongsooksai on 2 Jul 2019

I noticed that the pseudo code differs from the algorithm shown here:
Can you explain which one is correct?
I'm working on translating my R code to MATLAB environment, but somehow the inv() in MATLAB gives different values than solve() in R. I get to understand that R uses LU decomposition from LAPACK dgesv for every real matrix (in my case a symmetric covariance matrix). So if I want to replicate the solve() function in R, do I just use:
[L,U,P] = lu(A);
x = U \ (L \ (P*b));
to get the desired result?
For me, I don't care about precision nor speed, as long as the results from R is replicatable in MATLAB.


Sign in to comment.