4

I'm wondering how could I draw a matrix like that:

enter image description here

I know how to draw a simple matrix:

\documentclass[journal]{IEEEtran}

\usepackage[pdftex]{graphicx}

\usepackage{tikz}
\usetikzlibrary{matrix, calc}

\begin{document}

\begin{tikzpicture}

\matrix (input) [matrix of nodes,
                nodes={rectangle, draw=white, minimum size=.8cm}] at (0,0)
{
|[fill=black]| & |[fill=black!20]|  & |[fill=black!50]|         \\
|[fill=black!50]| & |[fill=black!50]|  & |[fill=black!20]|      \\
|[fill=black!20]| & |[fill=black!20]|  & |[fill=black]|         \\
};
\node [draw,below=8pt] at (input.south) {Sample};

\end{tikzpicture}

\end{document}

enter image description here

But not how to change it to achieve what I want.

Thank you.

1
  • Normally folks here like that you show what you have already tried - nice. -but you need to add a few lines to make your code compile-able: \documentclass{... \begin{document}... and do not write Thank you. Dec 8 '14 at 19:09
7

One answer using the rotation symetry of the figure :

\begin{tikzpicture}[thick, scale=.35]
  \draw[densely dotted, gray, shift={(-4.5,-4.5)}] (0,0) grid +(9,9);
  \draw (-4.5,-4.5) rectangle (4.5,4.5);
  \node {x};
  \foreach[count=\i] \a in {0,1,2,3} {
    \begin{scope}[rotate={90*\a}]
      \draw (4.5,3.5) -| ++(-1,-1) -| ++(-1,-1) -| ++(-1,-1) -- ++(-6,0);
      \path let \n1={int(2*\i-1)}, \n2={int(2*\i)} in (3,1) node{\n1} (2,3) node{\n2};
    \end{scope}
  }
\end{tikzpicture}

enter image description here

4
  • thank you for your answer. Could you explain me that line \draw (4.5,3.5) -| ++(-1,-1) -| ++(-1,-1) -| ++(-1,-1) -- ++(-6,0);? I'm trying to adapt it to reduce the matrix to a 5 x 5 size. I tried \draw (2.5,1.5) -| ++(-1,-1) -- ++(-4,0); but I think I misunderstood it.
    – pceccon
    Dec 9 '14 at 12:47
  • 1
    @pceccon Your understanding is good ! But you have also to replace all 4.5 by 2.5 and 9 by 5. The only thing that is not clear for me (because I don't know what you want exactly to obtain) is how to modify the number coordinates. Probably \path let \n1={int(2*\i-1)}, \n2={int(2*\i)} in (2,1) node{\n1} (1,2) node{\n2}; will be ok.
    – Kpym
    Dec 9 '14 at 17:24
  • Hi, @Kpym. After a considerable time, I was asked to paint each of those regions with a specific color. Is it possible to do this adapting your code? Thank you very much.
    – pceccon
    Apr 1 '15 at 20:26
  • 1
    @pceccon You can replace the first line (the grid) by something like \begin{scope}[shift={(-4.5,-4.5)}] \fill[red] (1,2) rectangle +(1,1); \fill[yellow] (4,5) rectangle +(1,1); \draw[densely dotted, gray] (0,0) grid +(9,9); \end{scope}. In general it is better to ask this kind of questions in a question, not in a comment.
    – Kpym
    Apr 2 '15 at 7:28
2

Just playing around (without tikz), building up stacked layers of \Sv (solid vertical), \Dv (dashed vertical), \Sh (solid horizontal), \Dh (dashed horizontal). Optional argument on vertical lines provides text following line

Quirks: \Sd (solid dot) needed in upper right corner of graph, due to the way I construct things. and \intersect may need to be defined to {.} if the \dashfill leaves the intersections blank.

\documentclass{article}
\def\LN{2ex}
\def\WD{1pt}
\usepackage{stackengine,xcolor,graphicx}

\def\intersect{}% might need it as {.}
\def\dashfill{\cleaders\hbox to 1.43pt{.}\hfill}
\newcommand\dashline[1]{\textcolor{black!50}{\hbox to #1{\dashfill\hfil}}}

\newcommand\Sh{\rule{\LN}{\WD}}

\newcommand\Sd{\rule{\WD}{\WD}}

\newcommand\Sv[1][]{%
  \rule{\WD}{\LN}\kern-\WD\smash{\rule[-\WD]{\WD}{\WD}}\kern-\WD%
    \raisebox{1pt}{\makebox[\LN]{#1}}}

\newcommand\Dh{\dashline{\LN}}

\newcommand\Dv[1][]{\makebox[\WD]{\rotatebox{90}\Dh}\kern-\WD%
    \smash{\makebox[\WD]{\raisebox{-\WD}{\textcolor{black!50}{\intersect}}}}\kern-\WD%
    \raisebox{1pt}{\makebox[\LN]{#1}}}

\setstackgap{S}{0pt}
\begin{document}
\Shortstack[l]{
  \Sh\Sh\Sh\Sh\Sh\Sh\Sh\Sh\Sh\Sd\\
  \Sv\Sv\Dv\Dv\Dv\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