TITLE: operation to get a diagonal matrix from a vector
QUESTION [5 upvotes]: In many programs you can create diagonal matrix from a vector, like diag function in Matlab and DiagonalMatrix function in Mathematica. I'm wondering whether we can use matrix product (or hadamard product, kronecker product, etc) of a vector and identity matrices to create a diagonal matrix.
Thank you!

REPLY [6 votes]: You can use
$$(xe^T) \odot I_n = \mathrm{diag}(x)$$
Where $\odot$ is the hadamard product and $e^T = (1,1,\ldots)\in\mathbb R^n$. The hadamard product basically masks away all off-diagonal elements and $xe^T$ has $x$ as its diagonal.