Home > utility > ls4abw.m

ls4abw

PURPOSE ^

Least-squares solution to a*b^T=w

SYNOPSIS ^

function [a, b]=ls4abw(w, plotOpt)

DESCRIPTION ^

 Least-squares solution to a*b^T=w
    Usage: [a, b]=ls4abw(w)
        This function returns the least-squares solution to a*b^T=w, where
        a: mx1 vector
        b: nx1 vector
        w: mxn matrix

    This function uses iterative least-squares to solve the problem.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [a, b]=ls4abw(w, plotOpt)
0002 % Least-squares solution to a*b^T=w
0003 %    Usage: [a, b]=ls4abw(w)
0004 %        This function returns the least-squares solution to a*b^T=w, where
0005 %        a: mx1 vector
0006 %        b: nx1 vector
0007 %        w: mxn matrix
0008 %
0009 %    This function uses iterative least-squares to solve the problem.
0010 
0011 %    Roger Jang, 20100229
0012 
0013 if nargin<1, selfdemo, return; end
0014 if nargin<2, plotOpt=0; end
0015 
0016 maxIter=100;
0017 tolerance=1e-10;
0018 [m, n]=size(w);
0019 a=ones(m,1);
0020 b=ones(n,1);
0021 obj=zeros(maxIter, 1);
0022 
0023 for i=1:maxIter
0024     aOld=a;
0025     bOld=b;
0026     a=w*b/(b'*b);
0027     b=w'*a/(a'*a);
0028     if norm(a-aOld)<tolerance & norm(b-bOld)<tolerance
0029         realIterCount=i-1;
0030         break;
0031     end
0032     error=a*b'-w;
0033     obj(i)=sum(sum(error.^2));
0034     if plotOpt
0035         fprintf('%d/%d: obj=%f, a=%s^T, b=%s^T\n', i, maxIter, obj(i), mat2str(a, 4), mat2str(b, 4));
0036     end
0037 end
0038 obj=obj(1:realIterCount);
0039 if plotOpt
0040     plot(1:realIterCount, obj, '.-');
0041 end
0042 
0043 % ====== Self demo
0044 function selfdemo
0045 w=rand(3, 4);
0046 %w = rand(120, 1)*rand(1, 100);
0047 plotOpt=1;
0048 [a, b]=feval(mfilename, w, plotOpt);

Generated on Tue 01-Jun-2010 09:50:34 by m2html © 2003