Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matrix division in Matlab

  1. Jul 17, 2014 #1


    User Avatar
    Gold Member

    Hello, I am confused about a concept,

    Suppose I am trying to solve a linear system ##Ax = b##

    I want to know why is it when I solve for x, the command is x = A\b. Why would it not be x = b\A. One could see that if you divide x on both sides, then b/x = A. Is this not true for the matrix?
  2. jcsd
  3. Jul 17, 2014 #2

    Simon Bridge

    User Avatar
    Science Advisor
    Homework Helper

    The correct operation is:

    The "A\" notation tells matlab to make the inverse of A and apply it to the following vector, using an efficient process coded into the m-file. It's just a notation.

    Division, like you did with b/x, is not defined for vectors and matrixes - which you should be able to tell by experimenting with a few examples.

    Code (Text):

    octave:41> A=magic(3)
    A =

       8   1   6
       3   5   7
       4   9   2

    octave:42> x=[1,2,3]'
    x =


    octave:43> b=A*x
    b =


    octave:44> b/x
    ans =

       2.0000   4.0000   6.0000
       2.4286   4.8571   7.2857
       2.0000   4.0000   6.0000
    ... clearly A≠b/x
    (Also try this by hand.)

    You should understand that matlab is a computer program which implements commands according to it's own internal logic. The full answer to your question is in how matlab interprets the forward-slash and backslash characters.

    See discussion:
    Last edited: Jul 17, 2014
  4. Jul 18, 2014 #3


    User Avatar
    Gold Member

    Matrix multiplication is not always commutative, like it is with scalars. So you can take for granted the fact that 5*3 = 3*5 = 15. But if you have something like the following it's different:

    Code (Text):

    A = [1 2; 3 5];
    B = [1 -1; -1 1];

    ans =

        -1     1
        -2     2


    ans =

        -2    -3
         2     3
    So ultimately this means that solving Ax = b and solving xA = b are two different problems.

    If you solve [itex]Ax = b[/itex], you get [itex]x = A^{-1}b[/itex]. Notice that A is on the left in each case.
    If you solve [itex]xA = b[/itex], you get [itex]x = bA^{-1}[/itex], and, as I just mentioned, this can be different from the form above. Here A is on the right.

    So, we have two different problems requiring 2 different operators.

    To solve Ax = b, you use x = A\b. That is, if A is on the left, use mldivide \.
    To solve xA = b, you use x = b/A. If A is on the right, use mrdivide /.

    (For more info, see http://blogs.mathworks.com/cleve/2013/08/19/backslash/ )

    MATLAB solves these equations quite efficiently depending on the properties of the coefficient matrix, A. A description of the algorithm is here at the bottom of the page: http://www.mathworks.com/help/matlab/ref/mldivide.html
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook