Matrix representation of operators - I
Let \(\hat A\) be an operator whose action on \(\mid \psi \rangle\) results in \(\mid \phi \rangle\):
\[\begin{equation} \label{eq1} \hat{A}|\psi\rangle =|\phi\rangle. \end{equation}\]Here, \(\mid \psi \rangle\) and \(\mid \phi \rangle\) are given by,
\[\begin{align} |\psi\rangle &=|+z\rangle\langle+z \mid \psi\rangle+|-z\rangle\langle-z \mid \psi\rangle \label{eq2} \\ |\phi\rangle &=|+z\rangle\langle+z \mid \phi\rangle+|-z\rangle\langle-z \mid \phi\rangle \label{eq3}. \end{align}\]Clealry, \(\mid \psi \rangle\) and \(\mid \phi \rangle\) are in a 2-dimensional vector space with \(|+z\rangle\) and \(|-z\rangle\) as basis vectors. \(\mid \psi \rangle\) and \(\mid \phi \rangle\) can be used to represent the intrinsic spin states of a spin-1/2 partcicle like the electron.
Inserting equations \eqref{eq2} and \eqref{eq3} into equation \eqref{eq1}, we get,
\[\begin{equation} \label{eq4} \hat{A}[\mid+z\rangle\langle+z \mid \psi\rangle+|-z\rangle\langle-z \mid \psi\rangle]=|+z\rangle\langle+z \mid \phi\rangle+|-z\rangle\langle-z \mid \phi\rangle. \end{equation}\]Take inner product of equation \eqref{eq4} with \(\langle+z\mid\) and \(\langle-z\mid\), successively.
\[\begin{align} \langle +z \mid \hat{A}|+z\rangle\langle+z \mid \psi\rangle+\langle+z|\hat{A}|-z\rangle\langle-z \mid \psi\rangle &=\langle+z \mid \phi\rangle \label{eq5} \\ % \langle-z \mid \hat{A}|+z\rangle\langle+z \mid \psi\rangle+\langle-z|\hat{A}|-z\rangle\langle-z \mid \psi\rangle &=\langle-z \mid \phi\rangle \label{eq6} \end{align}\]Equations \eqref{eq5} and \eqref{eq6} can be conveniently written in the form of a matrix:
\[\begin{equation} \label{eq7} \left( \begin{array}{ll} \langle+z|\hat{A}|+z\rangle & \langle+z|\hat{A}|-z\rangle \\ \langle-z|\hat{A}|+z\rangle & \langle-z|\hat{A}|-z\rangle \end{array} \right) % \left( \begin{array}{l} \langle+z \mid \psi\rangle \\ \langle-z \mid \psi\rangle \end{array} \right)= \left( \begin{array}{c} \langle+z \mid \phi\rangle \\ \langle-z \mid \phi\rangle \end{array} \right) % \end{equation}\]The column vectors on the LHS and RHS of equation \eqref{eq7} are in fact the matrix representations of \(\mid \psi \rangle\) and \(\mid \phi \rangle\) respectively. By comparing equation \eqref{eq7} with equation \eqref{eq1}, we can conclude that the square matrix on the LHS of equation \eqref{eq7} is indeed the matrix representation of the operator \(\hat{A}\) (in \(z-\) basis):
\[\begin{equation} \hat{A} \underset{S_z \text {-basis }}{\longrightarrow} \left( \begin{array}{ll} \langle+z|\hat{A}|+z\rangle & \langle+z|\hat{A}|-z\rangle \\ \langle-z|\hat{A}|+z\rangle & \langle-z|\hat{A}|-z\rangle \end{array} \right). \end{equation}\]If we label our basis vectors by \(\mid 1 \rangle\) and \(\mid 2 \rangle\) instead of \(\mid +z \rangle\) and \(\mid -z \rangle\), then the above square matrix can be written as:
\[\begin{equation} \begin{array}{l} A=\left(\begin{array}{cc} \langle 1|z| 1\rangle & \langle 1|z| 2\rangle \\ \langle 2|z| 1\rangle & \langle 2|z| 2\rangle \end{array}\right)=\left(\begin{array}{ll} A_{11} & A_{12} \\ A_{21} & A_{22} \end{array}\right), \end{array} \end{equation}\]where \(A\) is the matrix that represents the operator \(\hat{A}\). The \((ij)^{th}\) element of the matrix \(A\) can now be conveniently written as,
\[\begin{equation} A_{i j}=\langle i|\hat{A}| j\rangle. \end{equation}\]