TikZ - 计算数组为 2 个数组的最小值

TikZ - 计算数组为 2 个数组的最小值

我定义了两个数组r并画出来。

我现在想计算最小值r以便以类似的方式绘制它。 生成的数组将是 {0.2, 0.4, 0.8, 0.8, 0.4, 0.0, 0.1, 0.0, 0.1 ,0.5},但我想自动计算它。

你有什么主意吗 ?

以下是关于定义和绘制的 MWEr

\begin{tikzpicture}
  \def\r{{0.2}, {0.4}, {1.0}, {0.8}, {1.0}, {0.0}, {0.3}, {0.5}, {0.1}, {0.5}};
  \def\p{{0.9}, {0.5}, {0.8}, {0.8}, {0.4}, {0.3}, {0.1}, {0.0}, {0.8}, {0.7}};

  \filldraw (0, 0.2) node {$r$}
    \foreach \y [count=\x from 0] in \r { -- (\x, \y) circle[radius=1pt] };
  \filldraw (0, 0.9) node {$r$}
    \foreach \y [count=\x from 0] in \p { -- (\x, \y) circle[radius=1pt] };

\end{tikzpicture}

答案1

基本思想是遍历第一个列表并找到第二个列表的第 n 个成员,并将最小值保存到另一个列表中。

在此处输入图片描述

笔记:

参考:

代码:

\documentclass{article}
\usepackage{tikz}

%% https://tex.stackexchange.com/questions/14393/how-keep-a-running-list-of-strings-and-then-process-them-one-at-a-time
\makeatletter
\newcommand*\@NewList{}%
\newcommand{\ApendToNewList}[1]{%
    %% https://tex.stackexchange.com/questions/83595/expansion-issue-when-adding-to-csv-list-from-within-a-foreach
    \begingroup\edef\TempValue{\endgroup%
        \noexpand\g@addto@macro\noexpand\@NewList{\ifx\@NewList\empty\else,\fi#1}}\TempValue%
}

\newcommand*{\ExtractNthValueOfList}[3]{%
    \foreach [count=\i from 1] \x in #3 {%
        \ifnum\i=#2\relax
            \xdef#1{\x}%
            \breakforeach%
         \fi
    }%
}

\newcommand*{\@NthValue}{xx}
\newcommand*{\ExtractMinList}[3]{
    % #1 = variable to contain extract list
    % #2 = first list
    % #3 = second list
    \foreach [count=\j from 1] \y in #2 {%
        \ExtractNthValueOfList{\@NthValue}{\j}{#3}%
        \pgfmathsetmacro{\MinValue}{min(\y,\@NthValue)}%
        \ApendToNewList{\MinValue}%
        \typeout{\j, \y, \@NthValue}% <--- Useful for debugging
    }%
    \edef#1{\@NewList}%
}
\makeatother


\newcommand\RList{{0.2}, {0.4}, {1.0}, {0.8}, {1.0}, {0.0}, {0.3}, {0.5}, {0.1}, {0.5}}
\newcommand\PList{{0.9}, {0.5}, {0.8}, {0.8}, {0.4}, {0.3}, {0.1}, {0.0}, {0.8}, {0.7}}
\newcommand\ExtractedMinList{}% Ensure that we are not overwriting an existing macro

\begin{document}
    \ExtractMinList{\ExtractedMinList}{\RList}{\PList}%
    Extracted List=\ExtractedMinList
\end{document}

相关内容