# Drawing a matrix

I’m new to latex and I need your help with the matrix in the picture below:

mwe

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

\begin{document}

$\begin{blockarray}{ccccc} \begin{block}{c(cccc)} a & 2173.00 & 354.00 & 0.00 & 1249.00\smash[b]{\vphantom{\Big|}} \\ b & 0.00 & 3772.00 & 52.00 & 0.00 \\ c & 474.00 & 469.00 & 2858.00 & 42.00 \\ d & 1262.00 & 303.00 & 39.00 & 2180.00\smash[t]{\vphantom{\Big|}}\\ \end{block} & a & b & c & d \end{blockarray}$

\end{document}


Welcome to TeX-SE! You already have everything, you only need to make minor adjustments of the layout, and change the contents of the cells.

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

\begin{document}

$\begin{blockarray}{lccc} \text{Ratings} & \text{book}_1 & \text{book}_2 & \text{book}_3 \\ \begin{block}{l(ccc)} \text{user}_1 & - & 1 & - \smash[b]{\vphantom{\Big|}}\\ \text{user}_2 & - & 5 & - \\ \text{user}_3 & 4 & - & - \\ \text{user}_4 & - & - & 2\smash[t]{\vphantom{\Big|}}\\ \end{block} \end{blockarray}$
\end{document}


You could make the user and book texts italic (thanks to @naveganTeX for bringing this up!).

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

\begin{document}

$\begin{blockarray}{lccc} \text{Ratings} & \textit{book}_1 & \textit{book}_2 & \textit{book}_3 \\ \begin{block}{l(ccc)} \textit{user}_1 & - & 1 & - \smash[b]{\vphantom{\Big|}}\\ \textit{user}_2 & - & 5 & - \\ \textit{user}_3 & 4 & - & - \\ \textit{user}_4 & - & - & 2\smash[t]{\vphantom{\Big|}}\\ \end{block} \end{blockarray}$
\end{document}


A very quickly written tikz version:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning}

\begin{document}
\begin{tikzpicture}
\matrix [matrix of math nodes,left delimiter=(,right delimiter=),
row sep=0.2em,column sep=2em] (m) {
- & 1 & - \\
- & 5 & -  \\
4 & - & -  \\
- & - & 2\\};
\foreach \X in {1,...,4}
{\node[anchor=east] (u\X) at ([xshift=-1em]m.west|-m-\X-1){$\text{user}_\X$};}
\foreach \X in {1,...,3}
{\node[anchor=south] (b\X) at ([yshift=0.5em]m.north-|m-1-\X){$\text{book}_\X$};}
\draw ([yshift=0.3em]m.north west)  -- ++ (-4em,1em);
\node[anchor=south east] at ([yshift=0.02em]u1.north east) {Ratings};
\node[left=0.02em of b1,yshift=0.5em] {Movies};
\end{tikzpicture}
\end{document}


• Great! I wanted to ask you if the words “user” and “book” aren’t in math mode? – NaveganTeX Jul 6 at 7:32
• @naveganTeX This is a design choice. If anything one could replace \text by \textit for book and user, I think pure math mode would be "wrong". Writing answers is always a bit of a gamble, and my guess was that the one who produced the original screen shot used math mode only because they didn't know about the text command, but this may be a completely wrong guess. – user121799 Jul 6 at 7:32
• I would like to know if it is possible to draw a diagonal where it says “Ratings” and make it look like Movies\Ratings? – user192311 Jul 6 at 9:02
• @jassonpottersen Yes, it is possible but if you aim at adding more features of that type you may be better off if you draw the matrix with TikZ. See here for a bunch of option. – user121799 Jul 6 at 9:05
• Mmm, it is throwing me an error. It does not work with blkarray, maybe just with tables? – user192311 Jul 6 at 9:17