function Mar8()
% Solving a rank deficient LS problem choosing the LS minimzer to be
% as close as possible to a given vector.
m = 8; n = 6; r = 4;
% Generate a random mxn matrix with rank r...
A = randn(m,n);
[U,S,V] = svd(A);
% Set singular values to zero...
for k=r+1:n
    S(k,k) = 0;
end
A = U*S*V';
b = randn(m,1);
d = randn(n,1);
% Build up the SVD minimizer xLS...
xLS = zeros(n,1);
for k=1:r
    xLS = xLS + ((U(:,k)'*b)/S(k,k))*V(:,k);
end
% Set z = V(:,r+1:n)*y. We must solve
%      min(xLS + V(:,r+1:n)*y - d) = min(V(:,r+1:n)*y - (d-xLS))
% for y. 
% Note that the matrix of coefficients for this problem has orthonormal
% columns. Thinking about normal equations we see that
%       y = V(:,r+1:n)'*(d - xLS) = V(:,r+1:n)'*d.
x = xLS + V(:,r+1:n)*(V(:,r+1:n)'*d);
%