MATLAB: Rref gives an impossible answer

MATLABrref

So I am trying to figure out why the following code does give the correct answer. When calculating Q being the matrix P – eye(2), but defined by hand it returns the correct answer however when trying to do the above part, it returns the augmented identity matrix [1 0 0 ; 0 1 0]. I have no idea why it does not return what I expect.
P = [0.9871 0.0027; 0.0129 0.9973];
A = P - eye(2);
B = [A [0;0]];
rref(B)
Q = [-0.0129 0.0027; 0.0129 -0.0027];
rref([Q [0;0]])

Best Answer

I multiply all your array by 10000, so every inputs are integers
P = [9871 0027; 0129 9973];
A = P - 10000*eye(2);
B = [A [0;0]];
rref(B)
Q = [-0129 0027; 0129 -0027];
rref([Q [0;0]])
ans =
1.0000 -0.2093 0
0 0 0
ans =
1.0000 -0.2093 0
0 0 0
Now both agree (of course they are now identical when RREF is invoked).
I show you the result are correct by transforming B.
>> C=B/-129
C =
1.0000 -0.2093 0
-1.0000 0.2093 0
>> C(2,:)=C(2,:)+C(1,:)
C =
1.0000 -0.2093 0
0 0 0
Why you get different results? MATLAB RREF is anything but a serious, it uses rational approximation and fails miserably on floating points. Student is advised to use pencil+paper if not they might fail the exam.