Adding labels and comments to a matrix
Here's one possible option, using the fit
and positioning
libraries, and relative coordinates for the arrows on top. See comments in the code, ask if I should expand on anything.
\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{
matrix,
positioning, % added
fit % added
}
\begin{document}
\begin{tikzpicture}
\matrix (A) [matrix of math nodes, nodes in empty cells,
nodes={draw, minimum width=8mm, minimum height=5mm, outer sep=0pt, anchor=center},
row sep=-\pgflinewidth, column sep=-\pgflinewidth,
row 3/.style = {nodes={minimum height=8mm}},
row 5/.style = {nodes={minimum height=8mm}},
column 3/.style = {nodes={minimum width=10mm}},
row 4/.style = {nodes={fill=lightgray}},
row 6/.style = {nodes={fill=lightgray}},
column 7/.style = {nodes={minimum width=10mm}},]
{
& & & & & & \\
& & & & & & \\
& & & & & & \\
|[draw,fill=blue!20]|R & &|[draw,fill=blue!20]|R & & & ? & |[draw,fill=blue!20]|R \\
& & & & & & \\
|[draw,fill=blue!20]|R & &|[draw,fill=blue!20]|R & & & R & |[draw,fill=blue!20]|R \\
& & & & & & \\
& & & & & & \\
& & & & & & \\
};
% draw the box below
\node [draw,
text width=3cm,
below=of A,
name=textbox
] {Lorem ipsum dolor sit amet etc. etc. ad infinitum};
\foreach \i [count=\xi] in {1,2,\vdots,$i$, ,$j$,\vdots,$m-1$,$m$}
\node[left, font=\scriptsize,name=l-\xi] at (A-\xi-1.west) {\i\strut};
\foreach \i [count=\xi] in {1,2,\dots,$u$,\dots,$n-1$,$n$}
\node[above, font=\scriptsize] at (A-1-\xi.north) {\i\strut};
\foreach \i in {1,3,7}
{
% use the fit library instead of drawing box manually, then you get a node you can refer to
\node [fit=(A-4-\i)(A-6-\i), inner sep=-0.5mm, draw, dashed, name=R\i] {};
% draw arrow from textbox node
\draw [<-] (R\i.south) -- (textbox);
}
% draw arrows from the nodes using the names defined with the modification of the loop above
% and relative coordinates. Add helper coordinate on the first arrow
\draw [<-] (l-4) -- coordinate[pos=0.6] (s1) ++(-15mm,0);
\draw [<-] (l-6) -- ++(-15mm,0);
% draw arrow between the helper coordinate s1 and the point that has the x-coordinate of t-6 and the y-coordinate of s1
\draw [<->] (s1) -- node[fill=white,font=\scriptsize,inner sep=1pt] {$s_{i,j}=?$} (l-6 -| s1);
\end{tikzpicture}
\begin{tikzpicture}
\matrix (A) [matrix of math nodes, nodes in empty cells,
nodes={draw, minimum width=8mm, minimum height=5mm, outer sep=0pt, anchor=center},
row sep=-\pgflinewidth, column sep=-\pgflinewidth,
row 3/.style = {nodes={minimum height=8mm}},
row 5/.style = {nodes={minimum height=8mm}},
column 3/.style = {nodes={minimum width=10mm}},
column 4/.style = {nodes={fill=lightgray}},
column 6/.style = {nodes={fill=lightgray}},
column 7/.style = {nodes={minimum width=10mm}},]
{ & & & R & & ? & & & \\
& & & |[draw,fill=blue!20]|R & & |[draw,fill=blue!20]|R & & & \\
& & & & & & & & \\
& & & |[draw,fill=blue!20]|R & & |[draw,fill=blue!20]|R & & & \\
& & & & & & & & \\
& & & ? & & R& & & \\
& & & |[draw,fill=blue!20]|R & &|[draw,fill=blue!20]| R& & & \\};
% draw the box on the right
\node [draw,
text width=3cm,
right=of A,
name=textbox
] {Lorem ipsum dolor sit amet etc. etc. ad infinitum};
\foreach \i [count=\xi] in {1,2,\dots,$i$, ,$j$,\dots,$m-1$,$m$}
% added name=t-\xi to the following
\node[above, font=\scriptsize, name=t-\xi] at (A-1-\xi.north) {\i\strut};
\foreach \i [count=\xi] in {1,2,\vdots,$u$,\vdots,$n-1$,$n$}
\node[left, font=\scriptsize] at (A-\xi-1.west) {\i\strut};
\foreach \i in {2,4,7}
{
% use the fit library instead of drawing box manually, then you get a node you can refer to
\node [fit=(A-\i-4)(A-\i-6), inner sep=-0.5mm, draw, dashed, name=R\i] {};
% draw arrow from textbox node
\draw [<-] (R\i.east) -- (textbox);
}
% draw arrows from the nodes using the names defined with the modification of the loop above
% and relative coordinates. Add helper coordinate on the first arrow
\draw [<-] (t-4) -- coordinate[pos=0.6] (s1) ++(0,15mm);
\draw [<-] (t-6) -- ++(0,15mm);
% draw arrow between the helper coordinate s1 and the point that has the x-coordinate of t-6 and the y-coordinate of s1
\draw [<->] (s1) -- node[fill=white,font=\scriptsize,inner sep=1pt] {$s_{i,j}=?$} (t-6 |- s1);
\end{tikzpicture}
\end{document}
Very similar to Torbjørn T's nice answer except that the border of the comment is as in your figure (and drawn via path picture
, i.e. you can make it a style if you use such things more often).
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{matrix,fit,positioning}
\begin{document}
\begin{tikzpicture}
\matrix (A) [matrix of math nodes, nodes in empty cells,
nodes={draw, minimum width=8mm, minimum height=5mm, outer sep=0pt,
anchor=center},row sep=-\pgflinewidth, column sep=-\pgflinewidth,
row 3/.style = {nodes={minimum height=8mm}},
row 5/.style = {nodes={minimum height=8mm}},
column 3/.style = {nodes={minimum width=10mm}},
column 4/.style = {nodes={fill=lightgray}},
column 6/.style = {nodes={fill=lightgray}},
column 7/.style = {nodes={minimum width=10mm}},]
{ & & & R & & ? & & & \\
& & & |[draw,fill=blue!20]|R & & |[draw,fill=blue!20]|R & & & \\
& & & & & & & & \\
& & & |[draw,fill=blue!20]|R & & |[draw,fill=blue!20]|R & & & \\
& & & & & & & & \\
& & & ? & & R& & & \\
& & & |[draw,fill=blue!20]|R & &|[draw,fill=blue!20]| R& & & \\};
\foreach \i [count=\xi] in {1,2,\dots,$i$, ,$j$,\dots,$m-1$,$m$}
\node[above, font=\scriptsize] at (A-1-\xi.north) {\i\strut};
\foreach \i [count=\xi] in {1,2,\vdots,$u$,\vdots,$n-1$,$n$}
\node[left, font=\scriptsize] at (A-\xi-1.west) {\i\strut};
\node[right=1cm of A,align=left,text width=4cm,path picture={
\draw ([xshift=1em,yshift=-\pgflinewidth]path picture bounding box.north west) -|
([xshift=\pgflinewidth,yshift=\pgflinewidth]path picture bounding box.south west) -- ++ (1em,0);}] (txt){%
Some very long text about ducks, koalas and marmots which is very long and deals
with ducks, koalas and marmots. \dots};
\foreach \i in {2,4,7}
{\node[draw,rounded corners,fit=(A-\i-4)(A-\i-6),inner xsep=-1ex,
inner ysep=-0.3ex] (F-\i){};
\draw[-latex] (txt) -- (F-\i.east);
}
\end{tikzpicture}
\end{document}