在矩阵列上方添加节点

在矩阵列上方添加节点

我试图将符号放在矩阵的上方和列上,以及行的左侧。以下工作示例基本上就是我想要的,只是符号在括号内。我想把它们放在外面。

有什么方法可以实现这个吗?

\documentclass{article}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,positioning}

\begin{document}
    $A=$%
    \begin{tikzpicture}[baseline=(A.center)]
    \matrix (A) [matrix of math nodes, ampersand replacement=\&,
    left delimiter=(,right delimiter=)] 
    { {} \& {}  \& {}   \& {}   \& {} \\
      {} \& 1   \& 2    \& 17   \& 3 \\
      {} \& 4   \& 1    \& 24   \& 1 \\
      {} \& 0   \& 0    \& 2    \& -0.9 \\
      {} \& 0   \& 7    \& -0.95 \& 2 \\
    };

    \draw [black]  (A-1-2.west)  to (A-1-2.east);
    \draw [black,dashed]  (A-1-3.west)  to (A-1-3.east);
    \draw [black]  (A-1-4.west)--  plot[mark=square*, mark options={fill=black}] (A-1-4.center) -- (A-1-4.east);
    \draw [black]  (A-1-5.west)--  plot[mark=triangle*, mark options={fill=black}] (A-1-5.center) -- (A-1-5.east);

    \draw [black]  (A-2-1.west)  to (A-2-1.east);
    \draw [black,dashed]  (A-3-1.west)  to (A-3-1.east);
    \draw [black]  (A-4-1.west)--  plot[mark=square*, mark options={fill=black}] (A-4-1.center) -- (A-4-1.east);
    \draw [black]  (A-5-1.west)--  plot[mark=triangle*, mark options={fill=black}] (A-5-1.center) -- (A-5-1.east);
    \end{tikzpicture}%
\end{document}

答案1

我更新了关于 tikz 和矩阵的一个示例。您只需使用一些节点:这里和,A-1-1.west并且A-5-1.west您有几种放置符号的可能性。我过去常常xshift对行这样做,但对于列也一样yshift

   \node [xshift=-1.5 cm] at  (A-1-1.west)   {$\longrightarrow$ first row}; 
   \node [xshift=-1.5 cm] at  (A-5-1.west)   {$\longrightarrow$ last row};   


\documentclass[]{article}
\usepackage[utf8]{inputenc}
\usepackage[upright]{fourier}
\usepackage{tikz}
\usetikzlibrary{matrix,arrows,decorations.pathmorphing} 
\usepackage{fullpage}          
\begin{document}

\begin{tikzpicture}[>=latex]
% unit
\newcommand{\myunit}{1.2 cm}
% the styles
\tikzset{node style sp/.style={draw,circle,minimum size=\myunit}}  
\tikzset{node style ge/.style={circle,minimum size=\myunit}}
\tikzset{arrow style mul/.style={draw,sloped,midway,fill=white}}
\tikzset{arrow style plus/.style={midway,sloped,fill=white}}
% defintion of matrices
\matrix (A) [matrix of math nodes,%
             nodes = {node style ge},%
             left delimiter  = (,%
             right delimiter = )] at (0,0)
{%
  a_{11} &\ldots & a_{1k} & \ldots & a_{1p}  \\ 
    \vdots & \ddots & \vdots & \vdots & \vdots \\
  \node[node style sp] {a_{i1}};& \ldots%
         & \node[node style sp] {a_{ik}};%
                  & \ldots%
                           & \node[node style sp] {a_{ip}}; \\ 
  \vdots & \vdots& \vdots & \ddots & \vdots  \\
  a_{n1}& \ldots & a_{nk} & \ldots & a_{np}  \\ 
}; 
\node [draw,below] at (A.south) { $A$ : \textcolor{red}{$n$ rows} $p$ columns};
\matrix (B) [matrix of math nodes,%
             nodes = {node style ge},%
             left delimiter  = (,%
             right delimiter =)] at (7*\myunit,7*\myunit)
{% 
  b_{11} &  \ldots& \node[node style sp] {b_{1j}};%
                  & \ldots & b_{1q}  \\ 
  \vdots& \ddots & \vdots & \vdots & \vdots \\
  b_{k1} &  \ldots& \node[node style sp] {b_{kj}};%
                  & \ldots & b_{kq}  \\ 
  \vdots& \vdots & \vdots & \ddots & \vdots \\
  b_{p1} &  \ldots& \node[node style sp] {b_{pj}};%
                  & \ldots & b_{pq}  \\ 
};
\node [draw,above] at (B.north) { $B$ : $p$ rows \textcolor{red}{$q$ columns}}; 
% matrice résultat
\matrix (C) [matrix of math nodes,%
             nodes = {node style ge},%
             left delimiter  = (,%
             right delimiter = )] at (7*\myunit,0)
{% 
  c_{11} & \ldots& c_{1j} & \ldots & c_{1q} \\ 
  \vdots& \ddots & \vdots & \vdots & \vdots \\
    c_{i1}& \ldots & \node[node style sp,red] {c_{ij}};%
                  & \ldots & c_{iq} \\ 
  \vdots& \vdots & \vdots & \ddots & \vdots \\
  c_{n1}& \ldots & c_{nk} & \ldots & c_{nq} \\ 
}; 
\node [draw,below] at (C.south) {$ C=A\times B$ : \textcolor{red}{$n$ rows}  \textcolor{red}{$q$ columns}};
% arrows
\draw[blue] (A-3-1.north) -- (C-3-3.north);
\draw[blue] (A-3-1.south) -- (C-3-3.south);
\draw[blue] (B-1-3.west)  -- (C-3-3.west);
\draw[blue] (B-1-3.east)  -- (C-3-3.east);
\draw[<->,red](A-3-1) to[in=180,out=90] 
      node[arrow style mul] (x) {$a_{i1}\times b_{1j}$} (B-1-3) ;
\draw[<->,red](A-3-3) to[in=180,out=90]%
      node[arrow style mul] (y) {$a_{ik}\times b_{kj}$} (B-3-3);
\draw[<->,red](A-3-5) to[in=180,out=90]%
      node[arrow style mul] (z) {$a_{ip}\times b_{pj}$} (B-5-3);
\draw[red,->] (x) to node[arrow style plus] {$+\raisebox{.5ex}{\ldots}+$} (y)%
                  to node[arrow style plus] {$+\raisebox{.5ex}{\ldots}+$} (z);
                  %
                  % to (C-3-3.north west);
\draw[->,red,decorate,decoration=zigzag] (z) -- (C-3-3.north west);
\node [xshift=-1.5 cm] at  (A-1-1.west)   {$\longrightarrow$ first row}; 
\node [xshift=-1.5 cm] at  (A-5-1.west)   {$\longrightarrow$ last row}; 
\end{tikzpicture} 
\end{document}

在此处输入图片描述

答案2

您可以使用矩阵节点的锚点和一些数学知识来获取括号外的正确点。例如,您可以使用以下内容:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,positioning,calc}
\def\hoffset{1}
\def\voffset{.5}

\begin{document}
    $A=$%
    \begin{tikzpicture}[baseline=(A.center)]
    \matrix (A) [matrix of math nodes, ampersand replacement=\&,
    left delimiter=(,right delimiter=)] 
    { 1   \& 2    \& 17   \& 3 \\
      4   \& 1    \& 24   \& 1 \\
      0   \& 0    \& 2    \& -0.9 \\
      0   \& 7    \& -0.95 \& 2 \\
    };
    \draw [black]  ($(A-1-1.west)+(-\hoffset,0)$)  to ($(A-1-1.east)+(-\hoffset,0)$);
    \draw [black,dashed]  ($(A-2-1.west)+(-\hoffset,0)$)  to ($(A-2-1.east)+(-\hoffset,0)$);
    \coordinate (A31) at ($(A-3-1.center)+(-\hoffset,0)$);
    \coordinate (A41) at ($(A-4-1.center)+(-\hoffset,0)$);
    \draw [black]  ($(A-3-1.west)+(-\hoffset,0)$)--  plot[mark=square*, mark options={fill=black}] (A31)  -- ($(A-3-1.east)+(-\hoffset,0)$);
    \draw [black]  ($(A-4-1.west)+(-\hoffset,0)$)--  plot[mark=triangle*, mark options={fill=black}] (A41) -- ($(A-4-1.east)+(-\hoffset,0)$);

    \draw [black]  ($(A-1-1.west)+(0,\voffset)$)  to ($(A-1-1.east)+(0,\voffset)$);
    \draw [black,dashed]  ($(A-1-2.west)+(.1,\voffset)$)  to ($(A-1-2.east)+(0,\voffset)$);
    \coordinate (A13) at ($(A-1-3.center)+(0,\voffset)$);
    \coordinate (A14) at ($(A-1-4.center)+(0,\voffset)$);
    \draw [black]  ($(A-1-3.west)+(0,\voffset)$)--  plot[mark=square*, mark options={fill=black}] (A13) -- ($(A-1-3.east)+(0,\voffset)$);
    \draw [black]  ($(A-1-4.west)+(0,\voffset)$)--  plot[mark=triangle*, mark options={fill=black}] (A14) -- ($(A-1-4.east)+(0,\voffset)$);
    \end{tikzpicture}%
\end{document}

当然,您可以先定义矩阵外位置的坐标,然后直接使用它。如果您更经常需要这种东西,那么这样做可能会更简洁一些。修改此代码以满足您的需求应该非常简单。

带注释的矩阵

答案3

我想学习如何使用包blkarray,这里是结果。您的示例只需要TiKZ(已由 加载pgfplots)即可绘制标记符号,因为矩阵是用环境定义的blockarray

\documentclass{article}
\usepackage{pgfplots}
\usepackage{blkarray}

\begin{document}
\[A=
\begin{blockarray}{ccccc}
&\tikz{\draw (0,0)--++(0.5,0);}&\tikz{\draw[dashed] (0,0)--++(0.5,0);}&\tikz{\draw (0,0)--++(0.5,0) plot[mark=square*,mark options={fill=black}] (0.25,0);}&\tikz{\draw (0,0)--++(0.5,0) plot[mark=triangle*,mark options={fill=black}] (0.25,0);}\\
\begin{block}{c(cccc)}
\tikz{\draw (0,0)--++(0.5,0);}&1&2&17&3\\
\tikz{\draw[dashed] (0,0)--++(0.5,0);}&4&1&24&1\\
\tikz{\draw (0,0)--++(0.5,0) plot[mark=square*,mark options={fill=black}] (0.25,0);}&0&0&2&-0.9\\
\tikz{\draw (0,0)--++(0.5,0) plot[mark=triangle*,mark options={fill=black}] (0.25,0);}&0&7&-0.95&2\\
\end{block}
\end{blockarray}
\]
\end{document}

在此处输入图片描述

相关内容