Convert vector into diagonal matrix

$$\operatorname{diag} (\mathbf{x}) = \sum_{i=1}^n\mathbf{e}_i'\mathbf{x}\mathbf{e}_i\mathbf{e}_i'$$

Where $\mathbf{e}_i$ is the i-th basis vector of $\mathbb{R}^n$ and $'$ denotes the transpose.


I thought about this, and the best I can come up with is the following. It's about as fast as the standard matlab diag() function on small matrices, but I wasn't particularly rigorous. Anyway:

$$ v = v_i \in \mathbb{R}^n \\ D = diag(v) = D_{ii} \in \mathbb{R}^{n \times n} \\ D = \textbf{I}_n \cdot \left( \textbf{1}_n^T \otimes v \right) $$

In Matlab, this can be written as follows:

>> v = sym('v',[5 1])
D = eye(length(v)) .* kron( ones(length(v),1)',v )
v =
 v1
 v2
 v3
 v4
 v5
D =
[ v1,  0,  0,  0,  0]
[  0, v2,  0,  0,  0]
[  0,  0, v3,  0,  0]
[  0,  0,  0, v4,  0]
[  0,  0,  0,  0, v5]