Matrix of linear transformation in MATLAB

How can I determine a matrix $R$ in matlab such that, given a known matrix of coefficients $A$ gives me back its row reduced echelon form? Obviously I need an algorithm/function that works also with matrices that are not fully ranked.

$$RA=\text{rref}A$$ I don't know how to handle this... please can anybody help me? Thanks a lot! :)

• I suppose that you can find the echelon form and then solve a series of systems of equations. – nicoguaro Nov 8 '16 at 19:05
• The row reduced echelon form is computed by Gauss elimination, so an adapted LU factorization will do the trick (e.g., mathworks.com/matlabcentral/fileexchange/…). There are other rank revealing factorizations that might be more stable; see, e.g., math.sjsu.edu/~foster/rankrevealingcode.html. – Christian Clason Nov 10 '16 at 8:50
• rref is a blackboard method as in Gauss elimination. Numerically, in general they don't work as advertised for all cases. What you have in mind is usually done via LU or RRQR decompositions – percusse Nov 12 '16 at 9:53

Try this! Example here has rank 2 for a 3x3 matrix.

A = [1 1 1; 2 2 2; 5 7 6];
rrefA = rref(A);

disp(rrefA) % rref of A

C=[A eye(size(A))];

rrefC = rref(C);

R=rrefC(:,4:6);
disp(R*A) % should get rref of A
disp(R)

• Yeah, I have solved everything with the same approach! – james42 Nov 10 '16 at 16:29
• This will not work for non-square $A$, when the number of rows is greater than the number of columns. – Stefano M Nov 11 '16 at 6:53

I still cannot understand what this $R$ is good for, but in matlab you can simply (ab)use the slash operator:

>> [rrefA, jb] = rref(A);
>> R = rrefA(:,jb)/A(:,jb);


Here an example, showing that this method works also for non square matrices:

>> A = randn(10,3)*randn(3,4);
>> rank(A)

ans =

3

>> size(A)

ans =

10     4

>> [rrefA, jb] = rref(A);
>> R = rrefA(:,jb)/A(:,jb);
>> rank(R)

ans =

3

>> norm(R*A-rrefA)/norm(A)

ans =

1.7351e-15


Please note that this answer is just code golf: reduced row echelon form is not the most stable rank revealing factorisation, as noted above by Christian Clason.

You can solve the equation for $R$, then use the resulting term: $$R = rref(A)*A^{-1}$$

• This doesn't work when $A$ is not fully ranked – james42 Nov 9 '16 at 9:45