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.
- This is a simple matrix without any border
\[ \begin{matrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \\ \end{matrix} \]
- This code creates a bordered matrix with brackets
\[ \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \\ \end{bmatrix} \]
- 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} \]
- 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 \]
-
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 numberc
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} \]
-
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} \]
-
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
- Labels and captions of a matrix
- Labeling the Dimensions of a Matrix
- Matrix with column and row headings (duplicate)
Related Posts on Latex
You can find a comprehensive list of Latex resources in the following post:
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
- Add Copyright Notice and Conference Name in IEEE Conference Template
- Preparing Tables for Publication and Documentation in Latex
- Creating Bar Charts using Latex PGFPlots
- How to write an algorithm in Latex
- How to add subfigure in Latex
- How to Collaboratively Write a Paper using Overleaf Latex Platform
- Itemize, Enumerate, and To-do-list in Latex
- Latex Table for Survey in IEEE two-column format
- Line Plotting using Latex PGFPlots
- How to add Codes in Latex:
listings
package for code documentation
Leave a comment