删除矩阵中的一个元素

删除矩阵中的一个元素

我创建了一个矩阵,但是如何删除矩阵中的数字?

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{collcell}
\usepackage{hhline}
\usepackage{pgf}

\newcommand\gray{gray}

\newcommand\ColCell[1]{%
  \pgfmathparse{#1<.8?1:0}%
    \ifnum\pgfmathresult=0\relax\color{white}\fi
  \pgfmathparse{1-#1}%
  \expandafter\cellcolor\expandafter[%
    \expandafter\gray\expandafter]\expandafter{\pgfmathresult}#1}

\newcolumntype{E}{>{\collectcell\ColCell}c<{\endcollectcell}}

\begin{document}

\noindent\begin{tabular}{c*{6}{|E}|}
 \multicolumn{1}{c}{} & \multicolumn{1}{c}{hw} & \multicolumn{1}{c}{bx}
  & \multicolumn{1}{c}{wk} & \multicolumn{1}{c}{jg} & \multicolumn{1}{c}{cl}
  & \multicolumn{1}{c}{rn} \\ \hhline{~*6{|-}|}
 ving & 1 & 0 & 0 & 0 & 0 & 0 \\ \hhline{~*6{|-}|}
 xing & 0 & 0.92 & 0.08 & 0 & 0 & 0 \\ \hhline{~*6{|-}|}
 king & 0 & 0.03 & 0.97 & 0 & 0 & 0 \\ \hhline{~*6{|-}|}
 ging & 0 & 0 & 0 & 1 & 0 & 0 \\ \hhline{~*6{|-}|}
 ping & 0 & 0 & 0 & 0 & 1 & 0 \\ \hhline{~*6{|-}|}
 ning & 0 & 0 & 0 & 0 & 0 & 1 \\ \hhline{~*6{|-}|}
\end{tabular}\par\bigskip
\end{document}

答案1

使用幻影。

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{collcell}
\usepackage{hhline}
\usepackage{pgf}

\newcommand\gray{gray}

\newif\ifshowdata

\newcommand\ColCell[1]{%
  \pgfmathparse{#1<.8?1:0}%
  \ifnum\pgfmathresult=0\relax\color{white}\fi
  \pgfmathparse{1-#1}%
  \expandafter\cellcolor\expandafter[%
    \expandafter\gray\expandafter]\expandafter{\pgfmathresult}%
  \ifshowdata#1\else\phantom{#1}\fi
}

\newcolumntype{E}{>{\collectcell\ColCell}c<{\endcollectcell}}

\begin{document}

\noindent\begin{tabular}{c*{6}{|E}|}
 \multicolumn{1}{c}{} & \multicolumn{1}{c}{hw} & \multicolumn{1}{c}{bx} 
  & \multicolumn{1}{c}{wk} & \multicolumn{1}{c}{jg} & \multicolumn{1}{c}{cl} 
  & \multicolumn{1}{c}{rn} \\ \hhline{~*6{|-}|}
 ving & 1 & 0 & 0 & 0 & 0 & 0 \\ \hhline{~*6{|-}|}
 xing & 0 & 0.92 & 0.08 & 0 & 0 & 0 \\ \hhline{~*6{|-}|} 
 king & 0 & 0.03 & 0.97 & 0 & 0 & 0 \\ \hhline{~*6{|-}|}
 ging & 0 & 0 & 0 & 1 & 0 & 0 \\ \hhline{~*6{|-}|}
 ping & 0 & 0 & 0 & 0 & 1 & 0 \\ \hhline{~*6{|-}|}
 ning & 0 & 0 & 0 & 0 & 0 & 1 \\ \hhline{~*6{|-}|}
\end{tabular}

\bigskip

% now we show the data
\showdatatrue

\noindent\begin{tabular}{c*{6}{|E}|}
 \multicolumn{1}{c}{} & \multicolumn{1}{c}{hw} & \multicolumn{1}{c}{bx} 
  & \multicolumn{1}{c}{wk} & \multicolumn{1}{c}{jg} & \multicolumn{1}{c}{cl} 
  & \multicolumn{1}{c}{rn} \\ \hhline{~*6{|-}|}
 ving & 1 & 0 & 0 & 0 & 0 & 0 \\ \hhline{~*6{|-}|}
 xing & 0 & 0.92 & 0.08 & 0 & 0 & 0 \\ \hhline{~*6{|-}|} 
 king & 0 & 0.03 & 0.97 & 0 & 0 & 0 \\ \hhline{~*6{|-}|}
 ging & 0 & 0 & 0 & 1 & 0 & 0 \\ \hhline{~*6{|-}|}
 ping & 0 & 0 & 0 & 0 & 1 & 0 \\ \hhline{~*6{|-}|}
 ning & 0 & 0 & 0 & 0 & 0 & 1 \\ \hhline{~*6{|-}|}
\end{tabular}

\end{document}

在此处输入图片描述

答案2

这里我定义了一个Z列类型,它隐藏了内容,但仍添加了阴影。因此,E用以下内容替换该列Z

在此处输入图片描述

Y类型也会抑制阴影。您也可以根据需要进行混合搭配。例如,使用

\begin{tabular}{c |Z|Y|E|E|Z|Z|}

产量:

在此处输入图片描述

代码:

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{collcell}
\usepackage{hhline}
\usepackage{pgf}

\newcommand\gray{gray}

\newcommand\ColCell[1]{%
  \pgfmathparse{#1<.8?1:0}%
    \ifnum\pgfmathresult=0\relax\color{white}\fi
  \pgfmathparse{1-#1}%
  \expandafter\cellcolor\expandafter[%
    \expandafter\gray\expandafter]\expandafter{\pgfmathresult}#1}

\newcommand*{\DeleteEntry}[1]{%
  \hphantom{#1}%
}%
\newcommand*{\ColoredEmptyEntry}[1]{%
  \pgfmathparse{#1<.8?1:0}%
    \ifnum\pgfmathresult=0\relax\color{white}\fi
  \pgfmathparse{1-#1}%
  \expandafter\cellcolor\expandafter[%
    \expandafter\gray\expandafter]\expandafter{\pgfmathresult}\hphantom{#1}%
}%

\newcolumntype{E}{>{\collectcell\ColCell}c<{\endcollectcell}}
\newcolumntype{Y}{>{\collectcell\DeleteEntry}c<{\endcollectcell}}
\newcolumntype{Z}{>{\collectcell\ColoredEmptyEntry}c<{\endcollectcell}}


\begin{document}

\noindent\begin{tabular}{c |Z|Z|Z|Z|Z|Z|}
 \multicolumn{1}{c}{} & \multicolumn{1}{c}{hw} & \multicolumn{1}{c}{bx} 
  & \multicolumn{1}{c}{wk} & \multicolumn{1}{c}{jg} & \multicolumn{1}{c}{cl} 
  & \multicolumn{1}{c}{rn} \\ \hhline{~*6{|-}|}
 ving & 1 & 0 & 0 & 0 & 0 & 0 \\ \hhline{~*6{|-}|}
 xing & 0 & 0.92 & 0.08 & 0 & 0 & 0 \\ \hhline{~*6{|-}|} 
 king & 0 & 0.03 & 0.97 & 0 & 0 & 0 \\ \hhline{~*6{|-}|}
 ging & 0 & 0 & 0 & 1 & 0 & 0 \\ \hhline{~*6{|-}|}
 ping & 0 & 0 & 0 & 0 & 1 & 0 \\ \hhline{~*6{|-}|}
 ning & 0 & 0 & 0 & 0 & 0 & 1 \\ \hhline{~*6{|-}|}
\end{tabular}\par\bigskip

\noindent\begin{tabular}{c |Y|Y|E|E|Z|Z|}
 \multicolumn{1}{c}{} & \multicolumn{1}{c}{hw} & \multicolumn{1}{c}{bx} 
  & \multicolumn{1}{c}{wk} & \multicolumn{1}{c}{jg} & \multicolumn{1}{c}{cl} 
  & \multicolumn{1}{c}{rn} \\ \hhline{~*6{|-}|}
 ving & 1 & 0 & 0 & 0 & 0 & 0 \\ \hhline{~*6{|-}|}
 xing & 0 & 0.92 & 0.08 & 0 & 0 & 0 \\ \hhline{~*6{|-}|} 
 king & 0 & 0.03 & 0.97 & 0 & 0 & 0 \\ \hhline{~*6{|-}|}
 ging & 0 & 0 & 0 & 1 & 0 & 0 \\ \hhline{~*6{|-}|}
 ping & 0 & 0 & 0 & 0 & 1 & 0 \\ \hhline{~*6{|-}|}
 ning & 0 & 0 & 0 & 0 & 0 & 1 \\ \hhline{~*6{|-}|}
\end{tabular}\par\bigskip

\end{document}

相关内容