Suppose I have a $m\times n$ matrix $A$ (real for simplicity). Then SingularValueDecomposition[A]
yields 3 matrices $U$, $\Sigma$ and $V$ such that
$A = U\Sigma V^\top = u_1 \sigma_1 v_1^\top + u_2 \sigma_2 v_2^\top + \cdots$,
where $U = [u_1\;\; u_2 \ldots]$ (vector = column matrix).
Successive approximations for $A$ are given by the rank 1 matrices $u_1 \sigma_1 v_1^\top$, etc. I wanted to make a function to compute this but found myself doing all sorts of messy manipulations due to Mathematica's matrix structure (and/or my lack of knowledge on how to use them).
Here is my tentative function. It has as input any matrix A and an optional argument n saying how many rank 1 matrices should be summed. This function is not meant to be numerically fast or anything; perhaps I would use it as a educational tool to examine different approximations of A or etc. The point is in the Sum
function where I would like to know: is there a more efficient (or perhaps cleaner) way of doing the matrix multiplications?
RankOneApprox[A_, n_: 1] := Block[{U, Sig, V},
{U, Sig, V} = SingularValueDecomposition[A];
Sum[Sig[[i,i]] ({U[[All, i]]}\[Transpose].{V[[All, i]]}), {i, 1, n}]];