function [U,Q] = rq(S) S = S'; [Q,U] = qr(S(end:-1:1,end:-1:1)); Q = Q'; Q = Q(end:-1:1,end:-1:1); U = U'; U = U(end:-1:1,end:-1:1); if det(Q)<0 U(:,1) = -U(:,1); Q(1,:) = -Q(1,:); end return