I tried your code for this matrix
A = [2 3 4 5;6 7 8 9;10 11 12 13; 14 15 16 17];
and the result it's giving me is

1.0000 1.5000 2.0000 2.5000
0 1.0000 2.0000 3.0000
NaN NaN NaN NaN
NaN NaN NaN NaN

But when I tried it manually I mean with my hands I got zeros replacing of all the places with NaN. Kindly help.
Thanks

05 May 2008

Tim Davis

Warning ... this function does not pivot, which makes it useless for the general case.

The result R=ref(A) can be computed much more simply with [L,U]=lu(A) ; R = diag(diag(U))\U ; so this does not extend MATLAB in any useful way.

So perhaps the code is here just for educational use. However, codes like that must be well commented and well-written; this one is neither. It contains an absurd test (if (i == j)) which by inspection is always true. It contains gems of true absurdity such as statemients like a(i,:)= (1/a(i,j))* a(i,:); Why are you multiplying by the inverse of scalar? That is slow and inaccurate and hard to read. It uses n=length(a(1,:)) simply to determine the dimension of the input matrix a. Finally, the code is pure scalar code with lots of explicit loops.