0001 function [a, b]=ls4abw(w, plotOpt)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
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
0044 function selfdemo
0045 w=rand(3, 4);
0046
0047 plotOpt=1;
0048 [a, b]=feval(mfilename, w, plotOpt);