# MATLAB gotcha: operator precedence with backslash

## Alec Jacobson

## November 19, 2013

Matlab's backslash operator `\`

seemingly acts as a matrix inverse then multiply operator. So I often read:

```
x = A\b
```

as *x = A*^{-1} b. Indeed,

```
help \
```

reveals in matlab's documentation:

A\B is the matrix division of A into B, which is roughly the same as INV(A)*B, except it is computed in a different way.

This is very misleading when you consider:

```
x = 2*A\b;
```

If `A=1;b=1;`

then this sets `x`

to `0.5`

! This is because `\`

as a *division* operator does not take precedence over `*`

, and because it comes later in the expression this is equivalent to:

```
x = (2*A)\b;
```

which---so long as A is invertible--- is equivalent to:

```
x = 0.5*(A\b);
```

but decidedly not equivalent to:

```
x = 2*inv(A)*b;
```

or

```
x = 2*A^-1*b;
```

which both set `x`

to `2`

.