# Matrix with rows and columns labeled

It is trivial to make matrices like so:

\begin {pmatrix} s & t & a \\ c & k & x \\ cha & n & ge \end {pmatrix}


It is not so trivial to impose labels on the rows and columns as done in page 410 here. I would like to do this. For example, the matrix in 11.6:

Note: I am aware of this thread but this is not what I'm looking for.

Here are three different option using \bordermatrix, kbordermatrix and blkarray:

\documentclass{article}
\usepackage{amsmath,kbordermatrix,blkarray}

\renewcommand{\kbldelim}{(}
\renewcommand{\kbrdelim}{)}

\begin{document}
$\mathbf{P} = \bordermatrix{ & H & Y & D \cr H & .8 & .2 & .0 \cr Y & .3 & .4 & .3 \cr D & .2 & .1 & .7 } \qquad \mathbf{P} = \kbordermatrix{ & H & Y & D \\ H & .8 & .2 & .0 \\ Y & .3 & .4 & .3 \\ D & .2 & .1 & .7 } \qquad \mathbf{P} = \begin{blockarray}{cccc} & H & Y & D \\ \begin{block}{c(ccc)} H & .8 & .2 & .0 \\ Y & .3 & .4 & .3 \\ D & .2 & .1 & .7 \\ \end{block} \end{blockarray}$

\end{document}


The package nicematrix has tools to address that kind of problem.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

\NiceMatrixOptions
{
code-for-first-col = \scriptstyle ,
code-for-first-row = \scriptstyle
}

$\mathbf{P} = \begin{pNiceMatrix}[first-col,first-row] & H & Y & D \\ H & .8 & .2 & .0 \\ Y & .3 & .4 & .3 \\ D & .2 & .1 & .7 \\ \end{pNiceMatrix}$

\end{document}