# How to Write Matrix with Row or Column Labels in Latex

Sometimes, we have to create matrix while writing a math document in Latex. Here, I list a number of ways you can create matrices and also labeling the rows and columns if needed.

You can check the outputs in this Overleaf Document.

First, we need to include the following packages.

\usepackage{amsmath,blkarray,booktabs,bigstrut}


Now, let’s take a look at the codes for matrices.

1. This is a simple matrix without any border
 $\begin{matrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \\ \end{matrix}$

2. This code creates a bordered matrix with brackets
 $\begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \\ \end{bmatrix}$

3. A simple equation where we generate the dot product of two matrices
 $\begin{bmatrix} a & b & c \\ d & e & f \\ g & h & i \end{bmatrix} \cdot \begin{bmatrix} a & b & c \\ d & e & f \\ g & h & i \end{bmatrix} = \begin{bmatrix} a & b & c \\ d & e & f \\ g & h & i \end{bmatrix}$

4. A bordered matrix with row and column labels
 $\mathbf{\alpha} = \bordermatrix{ & \bar{f_1} & \bar{f_2} & \bar{f_3} \cr k_1 & 0 & 0 & 1 \cr k_2 & 1 & 0 & 0 \cr k_3 & 0 & 0 & 1 \cr k_4 & 0 & 1 & 0 } \qquad$

5. Another way to create a bordered matrix with row and column labels. I find this more helpful and easier than the other one. If you understand the code of writing a latex table, then this one is pretty simple. Use \begin{block}{c(ccc)} ... \end{block} to create the matrix, and then put the labels outside it using \begin{blockarray}{cccc} ... \end{blockarray}. Remember, the number of total column number c must be equal.

 $\mathbf{\beta} = \begin{blockarray}{cccc} & \bar{f_1} & \bar{f_2} & \bar{f_3} \\ \begin{block}{c(ccc)} f_1 & 3 & 2 & 0 \\ f_2 & 2 & 4 & 2 \\ f_3 & 5 & 3 & 1 \\ \end{block} \end{blockarray}$


Using (ccc) creates () shaped matrix while using [ccc] creates [] shaped matrix.

 $\begin{blockarray}{ccccc} & C_1 & C_2 & \dots & C_n \\ \begin{block}{c[cccc]} N_1 & a_{11} &a_{12} &\cdots & a_{1n}\bigstrut[t] \\ N_2 & a_{21}&a_{22}&\cdots &a_{2n} \\ & \vdots & \vdots & \ddots & \vdots\\ N_n & a_{n1}&a_{n2}&\cdots &a_{nn}\bigstrut[b]\\ \end{block} \end{blockarray}\vspace*{-1.25\baselineskip}$


Now, Let’s take a look at the all side labeling matrix

 $\gamma = \begin{blockarray}{cccccc} & \bar{f_1} & \bar{f_2} & \dots & \bar{f_n} \\ \begin{block}{c[cccc]c} k_1 & 0 & 1 &\cdots & 0\bigstrut[t] & =1 \\ k_2 & 1 & 0 &\cdots & 0 & =1 \\ & \vdots & \vdots & \ddots & \vdots &\\ k_n & 0 & 0 &\cdots & 1\bigstrut[b] & =1\\ \end{block} & \leq 4 & \leq 5 & \dots & \leq 10 & \\ \end{blockarray}\vspace*{-1.25\baselineskip}$

6. The following example follows the same way. The example is taken from this stackexchange thread.

 \newcommand{\cX}{\mathcal{X}}
\newcommand{\fixhd}[1]{
\smash[#1]{\vphantom{\Big|}}
}

 \begin{blockarray}{c ccccc c ccccc} & S_1 & \cdots & S_j & \cdots & S_N & & S_1 & \cdots & S_i & \cdots & S_N \\ \cmidrule{2-6} \cmidrule{8-12} \begin{block}{c [ccccc] c [ccccc]} T_1 & \fixhd{b} x_1 & \cdots & 0 & \cdots & 0 & T_{N+1} & 0 & \cdots & \cX_i^1 & \cdots & \cX_N^1 \\ \vdots & \vdots & \ddots & \vdots & \cdots & \vdots & \vdots & \vdots & \ddots & \vdots & \cdots & \vdots \\ T_j & 0 & \cdots & x_j & \cdots & 0 & T_{N+i} & \cX_1^i & \cdots & 0 & \cdots & \cX_N^i \\ \vdots & \vdots & \ddots & \vdots & \cdots & \vdots & \vdots & \vdots & \ddots & \vdots & \cdots & \vdots \\ T_N & 0 & \cdots & 0 & \cdots & x_N & T_{2N} & \cX_1^N & \cdots & \cX_i^N & \cdots & 0 \fixhd{t} \\ \end{block} \noalign{\vspace{-1.5ex}} & \BAmulticolumn{5}{c}{ \underbrace{\hphantom{\begin{bmatrix}x_1&\cdots&x_j&\cdots&x_N\end{bmatrix}}} _{\text{Broadcasting phase}} } && \BAmulticolumn{5}{c}{ \underbrace{\hphantom{\begin{bmatrix}\cX^N&\cdots&\cX^N&\cdots&\cX^N\end{bmatrix}}} _{\text{Cooperation phase}} } \\ \end{blockarray}

7. If you want to label the whole row or column, the following example is for you. This is a simple bmatrix with \underbrace{\begin{bmatrix} ... \end{bmatrix} }_{\displaystyle your-label} for columns and \left.\vphantom{ \begin{bmatrix} ... \end{bmatrix} }\right\} your-label for labeling rows on the right side.

 $\underbrace{ \begin{bmatrix} 1/2 & 1/2 & 0 & 0 & \cdots & 0\\ 0 & 1/2 & 1/2 & 0 & \cdots & 0\\ 0 & 0 & 1/2 & 1/2 & \cdots & 0\\ \vdots & \vdots & \vdots & \ddots & \vdots & \vdots\\ 1/2 & 0 & 0 & 0 & \cdots & 1/2\\ \end{bmatrix}}_{\displaystyle n} \left.\vphantom{\begin{bmatrix} 1/2 & 1/2 & 0 & 0 & \cdots & 0\\ 0 & 1/2 & 1/2 & 0 & \cdots & 0\\ 0 & 0 & 1/2 & 1/2 & \cdots & 0\\ \vdots & \vdots & \vdots & \ddots & \vdots & \vdots\\ 1/2 & 0 & 0 & 0 & \cdots & 1/2\\ \end{bmatrix}}\right\}m$


Once again, you can check the outputs in this Overleaf Document.

## References

You can find a comprehensive list of Latex resources in the following post:

Latex Resources in a Nutshell

If you are a new Latex user, check out this post: 20 Most Common Mistakes Made by New Latex Users

You can find all Latex oriented posts of mine in: https://shantoroy.com/categories/#latex

## Promotions and Referrals (US Residents Only)

• Chime: Open a Checking account at Chime using my referral link and get $100 after your employer deposit paycheck of minimum$200 within the first 45 days.
• Rakuten: Get $30 after you spend$30 at Rakuten select stores after you use my referral link to open an account.
• Chase Freedom Credit Card: Earn $200 cash back with Chase Freedom Unlimited or Chase Freedom Flex credit card. I can be rewarded if you apply using my referral link and are approved for the card. • Chase Checking Account: Get$200 when you open a checking account using my referral link after your first salary is deposited.
• Discover: Earn $50 cash back with Discover when you apply using my referral link and are approved for the card. • Amex Blue Cash Preferred: Earn$250 as statement credit when you spend \$3000 in first six months. Apply using my referral link.

Tags:

Categories:

Updated: