0

I am trying to get what's on this picture:

enter image description here

The problem is that when I add numbers the Q matrix, they don't look the same as in the original picture.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,
                positioning}
\tikzset{
    mymatrix/.style = {matrix of nodes,
                      nodes in empty cells,
                      nodes={minimum height=3ex, minimum width=1em,
                             inner sep=0pt, outer sep=0pt, anchor=center,
                             draw, very thin, fill=yellow},
                      column sep=0pt,
                      row sep=0pt,
                      inner sep=0.5\pgflinewidth, outer sep=1pt,
                      draw, thick},
 empty node/.style = {draw,fill=none},
every label/.append style = {font=\large, text=teal}
    }
\makeatletter
\def\tikz@lib@matrix@empty@cell{%
\iftikz@lib@matrix@empty%
\node[name=\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn,empty node]{};\fi}
\makeatother

\begin{document}
    \[
    \begin{tikzpicture}[node distance=0pt]
\matrix (m1) [mymatrix,
              label={[rotate=90, anchor=south]left: items},
              label=below:$R$,
              label=above:users]
{
1   &   & 3 &   &   & 5 &   &   & 5 &   & 4 &   \\
    &   & 5 & 4 &   &   & 4 &   &   & 2 & 1 & 3 \\
2   & 4 &   & 1 & 2 &   & 3 &   & 4 & 3 & 5 &   \\
    & 2 & 4 &   & 5 &   &   & 4 &   &   & 2 &   \\
    &   & 4 & 3 & 4 & 2 &   &   &   &   & 2 & 5 \\
1   &   & 3 &   & 3 &   &   & 2 &   &   & 4 &   \\
};
\node (approx) [right=of m1] {$\approx$};
\matrix (m2) [mymatrix=none, right=1em of approx,
              label={[rotate=90, anchor=south]left: items},
              label=below:$Q$,
              label=above:factors]
{
    0.1& -0.4  & -0.2 \\
    -0.5& 0.6 & 0.5 \\
    -0.2& 0.3 &0.5  \\
    1.1& 2.1 & 0.3 \\
    -0.7&2.1  & -2 \\
    -1& 0.7 & 0.3 \\
};
\node (times) [right=of m2] {$\times$};
\matrix (m3) [mymatrix=none, right=of times,
              label={[rotate=90, anchor=north]right: factors},
              label=below:$P^T$,
              label=above:users]
{
 1.1&-2  & 0.3 & 0.5 &-2  &  -0.5 & 0.8 & -0.4 & 0.3 & 1.4 & 2.4 &  -0.9\\
 -0.8& 0.7 & 0.5 & 1.4 &0.3  & -1 & 1.4 & 2.9 & 0.7 & -  1.2 & & 1.3\\
 2.1& -0.4 & 0.6 & 1.7 & 2.4 & .9 & -0.3 & 0.4 &  0.8 &   0.7 & -0.6&0.1 \\
};
\end{tikzpicture}
\]
\end{document}

enter image description here

I have problems with the size, also:

enter image description here

3

This is because the widths of the nodes were hardcoded. If you want to fix this with this code, you need to increase these widths.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,
                positioning}
\tikzset{
    mymatrix/.style = {matrix of nodes,
                      nodes in empty cells,
                      nodes={minimum height=3ex, minimum width=1em,
                             inner sep=0pt, outer sep=0pt, anchor=center,
                             draw, very thin, fill=yellow},
                      column sep=0pt,
                      row sep=0pt,
                      inner sep=0.5\pgflinewidth, outer sep=1pt,
                      draw, thick},
 empty node/.style = {draw,fill=none},
every label/.append style = {font=\large, text=teal}
    }
\makeatletter
\def\tikz@lib@matrix@empty@cell{%
\iftikz@lib@matrix@empty%
\node[name=\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn,empty node]{};\fi}
\makeatother

\begin{document}
    \[
    \begin{tikzpicture}[node distance=0pt]
\matrix (m1) [mymatrix,
              label={[rotate=90, anchor=south]left: items},
              label=below:$R$,
              label=above:users]
{
1   &   & 3 &   &   & 5 &   &   & 5 &   & 4 &   \\
    &   & 5 & 4 &   &   & 4 &   &   & 2 & 1 & 3 \\
2   & 4 &   & 1 & 2 &   & 3 &   & 4 & 3 & 5 &   \\
    & 2 & 4 &   & 5 &   &   & 4 &   &   & 2 &   \\
    &   & 4 & 3 & 4 & 2 &   &   &   &   & 2 & 5 \\
1   &   & 3 &   & 3 &   &   & 2 &   &   & 4 &   \\
};
\node (approx) [right=of m1] {$\approx$};
\matrix (m2) [mymatrix=none, right=1em of approx,
              label={[rotate=90, anchor=south]left: items},
              label=below:$Q$,
              label=above:factors,
              nodes={minimum width=2em}]
{
    0.1& -0.4  & -0.2 \\
    -0.5& 0.6 & 0.5 \\
    -0.2& 0.3 &0.5  \\
    1.1& 2.1 & 0.3 \\
    -0.7&2.1  & -2 \\
    -1& 0.7 & 0.3 \\
};
\node (times) [right=of m2] {$\times$};
\matrix (m3) [mymatrix=none, right=of times,
              label={[rotate=90, anchor=north]right: factors},
              label=below:$P^T$,
              label=above:users]
{
 &  &  &  &  &  &  &  &  &  &  &  \\
 &  &  &  &  &  &  &  &  &  &  &  \\
 &  &  &  &  &  &  &  &  &  &  &  \\
};
\end{tikzpicture}
\]
\end{document}

enter image description here

If you do not want to fill the cells in the second matrix yellow, add nodes={fill=yellow} only to the first matrix

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,
                positioning}
\tikzset{
    mymatrix/.style = {matrix of math nodes,
                      nodes in empty cells,
                      nodes={minimum height=3ex, minimum width=1em,
                             inner sep=0pt, outer sep=0pt, anchor=center,
                             draw, very thin,scale=0.9,transform shape},
                      column sep=0pt,
                      row sep=0pt,
                      inner sep=0.5\pgflinewidth, outer sep=1pt,
                      draw, thick},
 empty node/.style = {draw,fill=none},
every label/.append style = {font=\large, text=teal}
    }
\makeatletter
\def\tikz@lib@matrix@empty@cell{%
\iftikz@lib@matrix@empty%
\node[name=\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn,empty node]{};\fi}
\makeatother

\begin{document}
    \[
    \begin{tikzpicture}[node distance=0pt]
\matrix (m1) [mymatrix,
              label={[rotate=90, anchor=south]left: items},
              label=below:$R$,
              label=above:users,
              nodes={fill=yellow}]
{
1   &   & 3 &   &   & 5 &   &   & 5 &   & 4 &   \\
    &   & 5 & 4 &   &   & 4 &   &   & 2 & 1 & 3 \\
2   & 4 &   & 1 & 2 &   & 3 &   & 4 & 3 & 5 &   \\
    & 2 & 4 &   & 5 &   &   & 4 &   &   & 2 &   \\
    &   & 4 & 3 & 4 & 2 &   &   &   &   & 2 & 5 \\
1   &   & 3 &   & 3 &   &   & 2 &   &   & 4 &   \\
};
\node (approx) [right=of m1] {$\approx$};
\matrix (m2) [mymatrix=none, right=0.1em of approx,
              label={[rotate=90, anchor=south east,xshift=-0.7em]left: items},
              label=below:$Q$,
              label=above:factors,
              nodes={minimum width=2.4em}]
{
    0.1& -0.4  & -0.2 \\
    -0.5& 0.6 & 0.5 \\
    -0.2& 0.3 &0.5  \\
    1.1& 2.1 & 0.3 \\
    -0.7&2.1  & -2 \\
    -1& 0.7 & 0.3 \\
};
\node (times) [right=of m2] {$\times$};
\matrix (m3) [mymatrix=none, right=of times,
              label={[rotate=90, anchor=north]right: factors},
              label=below:$P^T$,
              label=above:users]
{
 &  &  &  &  &  &  &  &  &  &  &  \\
 &  &  &  &  &  &  &  &  &  &  &  \\
 &  &  &  &  &  &  &  &  &  &  &  \\
};
\end{tikzpicture}
\]
\end{document}

instead.

enter image description here

| improve this answer | |
  • Thank you Marmot! I've updated the OP. How can you make matrices Q and P^T look exactly as in the original picture? Meaning, with white background? – NaveganTeX Jun 27 '19 at 5:41
  • @naveganTeX This is written in the lower part of the answer. You need to add nodes={fill=none} or add fill=yellow only to the first matrix. – user121799 Jun 27 '19 at 5:42
  • 1
    @naveganTeX I fixed this by moving the label to where it is in your screen shot. – user121799 Jun 27 '19 at 5:50
  • 2
    The hyphens should be proper minus signs. – egreg Jun 27 '19 at 9:22
  • 1
    @egreg For that one only needs to replace matrix of nodes by matrix of math nodes. – user121799 Jun 27 '19 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.