TITLE: Constructing idempotent matrices
QUESTION [11 upvotes]: Is there a general method for constructing an idempotent matrix if we are given the values of the diagonal entries?

REPLY [6 votes]: The following should work "generically" (given a list of diagonal elements $d_i, i=1\ldots n$ where $\sum_{i=1}^n d_i = r$ is an integer, $1 \le r \le n-1$).  Start with a diagonal matrix with $r$ $1$'s and $n-r$ $0$'s on the diagonal.  First conjugate with a random matrix.
Then for $k$ from 1 to $n-1$, conjugate with a matrix that differs from the identity only in position $(k,k+1)$, chosen to make the $(k,k)$ element $d_k$.
For example, suppose we want a $3 \times 3$ idempotent matrix with diagonal elements $2,1,-1$.  Start with $\left(\matrix{1 & 0 & 0\cr 0 & 1 & 0\cr 0 & 0 & 0\cr}\right)$ and conjugate with $\left(\matrix{12 & -2 & 6\cr 15 & -7 & 3\cr 3 & 1 & -3\cr}\right)$ to get $M_0 = \left(\matrix {2/3 & -1/9 & 1/3\cr -1/2 & 5/6 & 1/2\cr 1/2 & 1/6 & 1/2\cr}\right)$.  The conjugate of $M_0$ by $\left(\matrix{1 & b & 0\cr 0 & 1 & 0\cr 0 & 0 & 1\cr}\right)$ has $(1,1)$ element $2/3 + b/2$, which is $2$ for $b = 8/3$.  Using this, we get $M_1 = \left(\matrix{2 & 3 & -1\cr
-1/2 & -1/2 & 1/2 \cr 1/2 & 3/2 & 1/2\cr}\right)$.  The conjugate of $M_1$ by $\left(\matrix{1 & 0 & 0\cr 0 & 1 & b\cr 0 & 0 & 1\cr}\right)$ has $(2,2)$ element $-1/2 - 3b/2$, which is $1$ for $b = -1$.  Using this, we obtain 
$M_2 = \left( \matrix{2 & 3 & -4\cr 0 & 1 & 0\cr 1/2 & 3/2 & -1\cr} \right)$, which satisfies the requirements.