Ways that Matlab Can Muddle You

 

1.  Using backslash to compute a matrix inverse.


Backslash (\) is an efficient way to compute a matrix inverse in Matlab, but it has one complication which throws nearly everyone.  Consider the equation:

    A x = y,
where we'll assume for now that A is an invertable square matrix.

This can be solved as:

    x = inv(A)*y
or
    x=A\y

Now consider

    A x = 10 y

You'd probably like to solve this as

    x = 10 A\ y

Don't do it!  Matlab computes a matrix containing everything to the left of the backslash before doing the inverse.  Thus x = 10A\y = inv(10*A)*y

To solve our equation, compute x = 10 (A\y) or x=10 * inv(A)*y
 

2.  Transpose and complex conjugate


We usually use a prime (') to compute a matrix transpose in Matlab.   This works well and produces the desired results.  If the vector y is complex, however, it's important to remember that the elements of y' are the complex conjugates of y.  This is mathematically correct and it's usually what you want for matrix manipulations.  It's not always the right thing for viewing or analyzing data.  To get a transpose without complex conjugate, use dot-prime (.').
 

Sarah Gille, 22 January 2003.