Matrix multiplied by its pseudo-inverse doesn't give the identity matrix. Why? Using Matlab, I randomly generate matrix $A \in \Bbb C^{2 \times 1}$ and compute its pseudo-inverse $A^{+}$. I notice that $AA^{+} \neq I$, and yet $\mbox{Tr}(AA^{+}) = 1$.
For other sizes it seems like the trace is equal to the smaller dimension of $A$. I couldn't find this property explained. Could anyone help me understand these two facts?
 A: Let $A \in \Bbb C^{m \times n}$ and $r := \mbox{rank} ({\rm A})$. Let the singular value decomposition (SVD) of $\rm A$ be
$${\rm A} = \begin{bmatrix} {\rm U}_1 & {\rm U}_2\end{bmatrix} \begin{bmatrix} \Sigma_1 & {\rm O}\\ {\rm O} & {\rm O}\end{bmatrix} \begin{bmatrix} {\rm V}_1^*\\ {\rm V}_2^*\end{bmatrix}$$
where $\Sigma_1$ is the $r \times r$ diagonal matrix whose diagonal entries are the positive singular values of $\rm A$. Note that $\rm A$ is invertible — assuming it is not empty, of course. Hence, the pseudo-inverse of $\rm A$ is
$${\rm A}^+ = \begin{bmatrix} {\rm V}_1 & {\rm V}_2\end{bmatrix} \begin{bmatrix} \Sigma_1^{-1} & {\rm O}\\ {\rm O} & {\rm O}\end{bmatrix} \begin{bmatrix} {\rm U}_1^*\\ {\rm U}_2^*\end{bmatrix}$$
and
$${\rm A} {\rm A}^+ = \begin{bmatrix} {\rm U}_1 & {\rm U}_2\end{bmatrix} \begin{bmatrix} {\rm I}_r & {\rm O}\\ {\rm O} & {\rm O}\end{bmatrix} \begin{bmatrix} {\rm U}_1^*\\ {\rm U}_2^*\end{bmatrix} = {\rm U}_1 {\rm U}_1^*$$
is a projection matrix. Note that ${\rm A} {\rm A}^+ = {\rm U}_1 {\rm U}_1^* = {\rm I}_m$ if and only if matrix $\rm A$ has full row rank. Moreover, the trace is
$$\mbox{tr} \left( {\rm A} {\rm A}^+ \right) = \mbox{tr} \left( {\rm U}_1 {\rm U}_1^* \right) = \mbox{tr} \left( {\rm U}_1^* {\rm U}_1 \right) = \mbox{tr} \left( {\rm I}_r \right) = r = \mbox{rank} ({\rm A})$$
Let $\rm P$ be a projection matrix. Then, $\mbox{tr} \left( {\rm P} \right) = \mbox{rank} ({\rm P})$.  A very nice property of projection matrices.
A: Suppose you generate $A\in\mathbb{C}^{m\times n}$ randomly, as you specified. Then $\text{rank}(A)=\min\{m,n\}$ with high probability (depending on the probability distribution you used, of course). Now, if $A$ has rank $m$, then $AA^*\in\mathbb{C}^{m\times m}$ has rank $m$ and is invertible. Therefore, the Moore-Penrose pseudoinverse $A^\dagger = A^*(AA^*)^{-1}$ can be used to actually "obtain" $I$ through right-inversion, i.e., $AA^\dagger=I_m$. Similarly, if $\text{rank}(A)=n$, then $A^*A\in\mathbb{C}^{n\times n}$ is invertible, and $A^\dagger = (A^*A)^{-1}A^*$ is a left inverse of $A$; $A^\dagger A = I_n$.
Note that if $\text{rank}(A)=n<m$, then for all $B\in\mathbb{C}^{n\times m}$ it holds that
$$\text{rank}(AB)\le \min\{\text{rank}(A),\text{rank}(B)\}=\min\{n,\text{rank}(B)\}\le n < m$$
and therefore the matrix $AB\in\mathbb{C}^{m\times m}$ cannot possibly equal the identity (which has rank $m$). A similar remark holds in the case that $\text{rank}(A)=m<n$, namely, that there is no left inverse.
Therefore, for your example of $A\in\mathbb{C}^{2\times 1}$, you cannot expect there to be a right inverse of $A$, since $AB$ will be of rank-1 for all $B\in\mathbb{C}^{1\times 2}$. However, you should be able to find a left inverse (a vector that has inner product equal to one with $A$).
As per your question regarding trace: are you using MATLAB's pinv function? If so, it will compute whichever Moore-Penrose inverse "makes sense". In other words, for your matrix $A\in\mathbb{C}^{2\times 1}$, the MATLAB function will compute a left pseudo-inverse $A^\dagger\in\mathbb{C}^{1\times 2}$. In this case, $A^\dagger A$ should equal to $I_1=1$. Furthermore, by the cyclic property of the trace, using the left inverse on the "wrong side" (the right), you will still get a trace of one, since $\text{tr}(AA^\dagger)=\text{tr}(A^\dagger A) = \text{tr}(1)=1$.
More generally, suppose $A\in\mathbb{C}^{m\times n}$ has rank $n<m$ (and therefore is left-invertible). Then the left Moore-Penrose pseudoinverse $A^\dagger=(A^*A)^{-1}A^*$ when used on the right will give
$$\text{tr}(AA^\dagger) = \text{tr}(A^\dagger A) = \text{tr}((A^*A)^{-1}A^*A)=\text{tr}(I_n) = n$$
as per your observation.
