# Changing individual elements of a matrix in Tikz

I am preparing lecture notes and I need to manipulate a matrix between iteration. Typically, only few elements change. Therefore, it would be nice to change these elements instead of copying a matrix again, and again. I would be grateful for your insights.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,arrows,fit,automata}

\begin{document}

\begin{tikzpicture}

\matrix(rij) [matrix of math nodes, nodes in empty cells, nodes={minimum size=1cm, outer sep=0pt, text height=1.5ex, text depth=.25ex}]
{
6      &  11    &   5    & 0  \\
4      &   3    &   3    & M  \\
3      &   6    &   7    & 0  \\
3      &   7    &   8    & M  \\
M      &   0    &   0    & 0  \\
};

\end{tikzpicture}
\end{document}

• It is very easy to manipulate matrix elements. But I do not understand the question. (1) which "iteration" are you referring to in "I need to manipulate a matrix between iteration"? (2) What should be changed to what? – user194703 Jun 3 at 17:10
• For instance, I would like to the add a 2 to the element in the first line and second column (1,2) and it would read 13 instead of 11. – Johan Wideberg Jun 3 at 17:45

Here is an example: draw the matrix in a pic, and then set the value by some key value to be changed. The benifit is that you can access the coordinate of the matrix as usual.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,arrows,fit,automata,positioning}
\tikzset{
value to be changed/.store in=\anytl,
value to be changed=11, % default value,
mymatrix/.pic={
\matrix(-rij) [
matrix of math nodes,
nodes in empty cells,
nodes={
minimum size=1cm,
outer sep=0pt,
text height=1.5ex,
text depth=.25ex
},
ampersand replacement=\&
]
{
6      \&  \anytl\&   5    \& 0  \\
4      \&   3    \&   3    \& M  \\
3      \&   6    \&   7    \& 0  \\
3      \&   7    \&   8    \& M  \\
M      \&   0    \&   0    \& 0  \\
};
\coordinate (-south) at (current bounding box.south);
}
}

\begin{document}
\begin{tikzpicture}
\pic (a) at (0, 0) {mymatrix};
\pic [value to be changed=change, below=of a-south] {mymatrix};
\end{tikzpicture}
\end{document} You can perform operations on the entries of the matrix, with may depend on the counts \pgfmatrixcurrentrow and \pgfmatrixcurrentcolumn. The matrix content are stored in \myarray. If you just say

\mymatrix


you will get the corresponding matrix, but if you say e.g.

\mymatrix[\ifnum\pgfmatrixcurrentcolumn=2
\pgfmathparse{int(\pgfmatrixcurrentrow+\originalentry)}\pgfmathresult
\else
\originalentry
\fi]


you will add the row index to the entries of the second column.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\edef\myarray{{"6","11","5","0"},%
{"4","3","3","M"},%
{"3","6","7","0"},%
{"3","7","8","M"},%
{"M","0","0","0"}}
\newcommand\mymatrix[\originalentry]{\begin{tikzpicture}
\matrix(rij) [matrix of math nodes, nodes in empty cells,
nodes={minimum size=1cm, outer sep=0pt, text height=1.5ex, text depth=.25ex},
cells={nodes={execute at begin node={\pgfmathsetmacro\originalentry{{\myarray}[\pgfmatrixcurrentrow-1][\pgfmatrixcurrentcolumn-1]}%
#1}}},
ampersand replacement=\&]
{
\& \& \& \\
\& \& \& \\
\& \& \& \\
\& \& \& \\
\& \& \& \\
};
\end{tikzpicture}}
\begin{document}
\mymatrix
\bigskip

\mymatrix[\ifnum\pgfmatrixcurrentcolumn=2
\pgfmathparse{int(\pgfmatrixcurrentrow+\originalentry)}\pgfmathresult
\else
\originalentry
\fi]
\end{document} • It is almost what I need to do. But I would rather see the results instead, for stance 12 and not 1+11 – Johan Wideberg Jun 3 at 17:47