# Different results for row reduction in Matlab

Consider the following matrix: $\left[ \begin{array}{ccc} -0.05 & 0.45 & 0 \\ 0.05 & -0.45 & 0 \end{array} \right]$

Row reducing the above matrix in Matlab using the rref() function produces what I would expect (just adding top row to bottom row and scaling top row): $\left[ \begin{array}{ccc} 1.0 & -9.0 & 0 \\ 0 & 0 & 0 \end{array} \right]$

But if I remove the last column of just zeros, and row reduce that matrix, I get a 2x2 identity matrix: $\left[ \begin{array}{ccc} -0.05 & 0.45 \\ 0.05 & -0.45 \end{array} \right] \sim \left[ \begin{array}{ccc} 1 & 0 \\ 0 & 1 \end{array} \right]$

I can't see how removing the last column changes anything; adding the top row to the bottom row will still produce the result above, just without the last column of $0$'s. But I'm quite sure Matlab is right and I'm not, so what am I missing here?

Edit: I have managed to reproduce the above and I believe it's all due to rounding errors. If you input $M = \left[ \begin{array}{ccc} 0.95 & 0.45 \\ 0.05 & .55 \end{array} \right]$ and then do $A = M - eye(2)$. rref(A) will now give the $2 \times 2$ identity matrix. If I enter the result of $M-eye(2)$ directly, that is $B= \left[ \begin{array}{ccc} -0.05 & 0.45 \\ 0.05 & -0.45 \end{array} \right]$, then rref(B) returns the expected $\left[ \begin{array}{ccc} 1 & -9 \\ 0 & 0 \end{array} \right]$ .

Here's a screenshot as an example:

-
Are you sure you're getting an identity matrix for the $2\times 2$ case? The matrix you have there is singular, after all. –  Guess who it is. Oct 20 '11 at 13:35
I've just introduced your matrix on my Matlab and said to row reduce it with rref(A) and the result is the correct one: your second matrix, without the third column of zeros. –  a.r. Oct 20 '11 at 13:48
@J.M. and Agusti, I think I might have narrowed it down to a rounding error, causing the top and bottom row to be slightly different (but not shown due to lack of decimals shown in Matlab). As you are both more experienced here than me, do you think I should answer my own post or delete it? Thank you! –  Jodles Oct 20 '11 at 15:40
What version of MATLAB are you using? No, don't delete; this might be a nice cautionary tale... (You might want to post a screenshot of what you gave MATLAB as input.) –  Guess who it is. Oct 20 '11 at 15:48
It's a cautionary example for using rank functions. For the example above rank(A) returns 1, but rref(A) thinks it's 2. You can tweak it though >> rref(A) ans = 1 0 0 1 >> rref(A,1E-15) ans = 1.0000 -9.0000 0 0 >> I'm surprised that the default eps value ends up with wrong result as well. –  karakfa Oct 20 '11 at 17:00

This is already documented in MATLAB.

Roundoff errors may cause this algorithm to compute a different value for the rank than rank, orth and null.

You need to type format long e. After that you can see the difference if you execute N-eye(2) resulting with

N-eye(2)

ans =

-5.000000000000004e-002    4.500000000000000e-001
5.000000000000000e-002   -4.500000000000000e-001


Here, the trouble is already visible in the (1,1) element of the matrix. But also

 [1 1]*(N-eye(2))

ans =

-4.163336342344337e-017    5.551115123125783e-017


gives you the error between seemingly identical elements.

The reason why you get correct results with an additional zero column is (my personal view) due to the term in the tolerance computation for rref given by (max(size(A))*eps *norm(A,inf)). Here, the first term is 3 instead of 2 and that should make the small difference between selecting a rank 1 and rank 2 matrix.

-