# How do you work out the matrix for controlled-U operations?

I see this equations all over for controlled-U operations:

$$\left|{0}\right>\left<{0}\right| \otimes \mathbf{1} + \left|{1}\right>\left<{1}\right|\otimes U = \begin{pmatrix} \mathbb{1} & 0 \\ 0 & U \end{pmatrix}$$

I have been trying to work this out and can not figure out where I'm going wrong:

\begin{align} \left|{0}\right>\left<{0}\right| \otimes \mathbf{1} + \left|{1}\right>\left<{1}\right|\otimes U &= \begin{pmatrix} 0 \\ 0 \end{pmatrix}\begin{pmatrix} 0 & 0 \end{pmatrix} \otimes \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix} + \begin{pmatrix} 1 \\ 1 \end{pmatrix}\begin{pmatrix} 1 & 1 \end{pmatrix} \otimes U\\ &= \begin{pmatrix} 0 & 0 \\ 0 & 0 \end{pmatrix} \otimes\begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix} + \begin{pmatrix} 1 & 1 \\ 1 & 1 \end{pmatrix} \otimes U \\ &= \begin{pmatrix} 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \end{pmatrix} + \begin{pmatrix} U & U \\ U & U \end{pmatrix} \\ &= \begin{pmatrix} U & U \\ U & U \end{pmatrix} \neq \begin{pmatrix} \mathbb{1} & 0 \\ 0 & U \end{pmatrix} \end{align}

Even checking with python doing the calculations, I get the wrong answer. Where am I going wrong? Or can someone explain what I'm missing?

$$\left|0\right>$$ and $$\left|1\right>$$ don't mean $$\begin{pmatrix}0 \\ 0\end{pmatrix}$$ and $$\begin{pmatrix}1 \\ 1\end{pmatrix}$$.
In bra-ket notation we usually fix some basis and denote by $$\left|a\right>$$ the element of this basis labeled by the symbol $$a$$. In the case of a qubit $$\left|0\right>$$ and $$\left|1\right>$$ denote the basis vectors $$\begin{pmatrix} 1 \\ 0 \end{pmatrix}$$ and $$\begin{pmatrix}0 \\ 1\end{pmatrix}$$