TITLE: Mathematical expression to form a vector from diagonal elements
QUESTION [8 upvotes]: I would like to know is there any way to express mathematically (using matrices multiplication, addition, etc.) a vector which is formed from the diagonal elements of a matrix. For example, I have a matrix called $\mathbf{M}$ and I want to create a vector  $\mathbf{v}$ such that
$\mathbf{v}= \text{diagonal elements of } \mathbf{M} $
Any matrix algebra or operation do that ? Thank you.

REPLY [5 votes]: Let $e_i$ be a basis vector of the target n-dimensional vector space. Let $E_{ij}$ be the matrix with 1 in the ij'th position -- a basis "vector" in the space of n by n matrices where we also introduce Frobenius inner product. Then, $$D(M)=\sum_{i=1}^n\langle M,E_{ii}\rangle e_i.$$ It is essentially a projection operator onto the subspace $\{E_{ii}\}$.