Path: news.mathworks.com!not-for-mail
From: "Bruno Luong" <b.luong@fogale.findmycountry>
Newsgroups: comp.soft-sys.matlab
Subject: Re: RQ decomposition
Date: Sun, 5 Oct 2008 13:32:01 +0000 (UTC)
Organization: FOGALE nanotech
Lines: 61
Message-ID: <gcafkh$9uh$1@fred.mathworks.com>
References: <gc82pk$e87$1@fred.mathworks.com> <gc8hqd$9ja$1@fred.mathworks.com> <gc8uop$2nh$1@fred.mathworks.com> <gc98e6$mbt$1@fred.mathworks.com>
Reply-To: "Bruno Luong" <b.luong@fogale.findmycountry>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1223213521 10193 172.30.248.35 (5 Oct 2008 13:32:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sun, 5 Oct 2008 13:32:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 390839
Xref: news.mathworks.com comp.soft-sys.matlab:493692


I have extended my rq with more options:

function [R Q]=rq(A, varargin)
% function [R Q]=rq(A)
%
% Perform RQ factorization
%   A [m x n]
%   returns R [m x n] triangular superior and
%           Q [n x n] unitary (i.e., Q'*Q=I)
%   such that A = R*Q
%
% Note: Standard RQ requires m<=n.
%
% For n ~=m, default output R has non-zero elements populate
%   at the m-first rows
% Use [R Q] = rq(A, 'last') to populate non-sero R-rows at other end.
%
% For "non standard" RQ, i.e. for A with m > n, zero are padded in the
%    outputs R and Q. However Q is no longer unitary. More precisely
%    Q'*Q = eye(n) but Q*Q' is
%      diag([zeros(1,m-n) ones(1,n)]) without 'last' option, and
%      diag([ones(1,n) zeros(1,m-n)]) with 'last' and R becomes triangular.
%
% Author: Bruno Luong
% last Update: 05/Oct/2008

[m n]=size(A);

[Q R]=qr(flipud(A).');
R=flipud(R.'); % m x n
Q=Q.'; % n x n
if m>n
    warning('RQ:DimensionBizarre',...
            'RQ: number of rows is larger the number of columns'); 
    % Padding zeros
    R(end,m)=0; % m x m
    Q(m,end)=0; % m x n
end

R(:,1:m)=R(:,m:-1:1);
Q(1:m,:)=Q(m:-1:1,:);

last=strcmpi(getoption('',varargin{:}),'last');
% Populate R at last rows
if xor(m>n,last)
    R=circshift(R,[0 n-m]);
    Q=circshift(Q,[n-m 0]);
end

end

% Get option if provided
function res=getoption(default, option)
  if nargin<2 || isempty(option)
      res=default;
  else
      res=option;
  end
end

% Bruno