從每一行中減去第一行並計算相對差異

從每一行中減去第一行並計算相對差異

我試圖透過從每一行中減去第一列來從另一列建立一個新列。然後我想建立另一列具有相對差異的列。

\documentclass{article}
\usepackage{pgfplotstable}
\begin{document}
\pgfplotstableread{
TotalDistance
35089
35182
35410
35523
35694
35789
35895
35984
36006
36068
}{\mytable}
\pgfplotstabletypeset[
    columns={TotalDistance,LocalDistance},
    create on use/LocalDistance/.style={
        create col/expr={\thisrow{TotalDistance} - 35089},
    },
    create on use/DifferenceDistance/.style={
        create col/expr={\thisrow{LocalDistance} - \prevrow{LocalDistance}},
    },
    ]{\mytable}
\end{document}

我的目標是獲得:

  TD  | LD  | DD
------+-----+----
35089 |   0 |   0
35182 |  93 |  93
35410 | 321 | 228
35523 | 434 | 113
35694 | 605 | 171
35789 | 700 |  95
35895 | 806 | 106
35984 | 895 |  89
36006 | 917 |  22
36068 | 979 |  62

目前存在的問題有:

  1. 如何避免硬編碼35089,而是從第一個單元格讀取它?
  2. 我收到錯誤未知函數“LocalDistance__column_not_found”當我嘗試排版 DifferenceDistance 列時,即使建立 LocalDistance 列也沒有問題。

答案1

  1. 您可以使用 讀取第一個儲存格值\pgfplotstablegetelem{<row>}{<col>}\of\mytable。返回值將被寫入\pgfplotsretval

  2. 根據提議打擊樂,這裡是一個使用\pgfplotstablecreatecol而不是\pgfplotstabletypeset向表中添加列的範例:

在此輸入影像描述

\documentclass{article}
\usepackage{pgfplotstable}

\begin{document}

\pgfplotstableread{
TotalDistance
35089
35182
35410
35523
35694
35789
35895
35984
36006
36068
}{\mytable}

% read the first cell value -> \pgfplotsretval
\pgfplotstablegetelem{0}{TotalDistance}\of\mytable

% add column LocalDistance
\pgfplotstablecreatecol
    [expr={\thisrow{TotalDistance} - \pgfplotsretval}]
    {LocalDistance}{\mytable}

% add column DifferenceDistance
\pgfplotstablecreatecol
    [expr={\thisrow{LocalDistance} - \prevrow{LocalDistance}}]
    {DifferenceDistance}{\mytable}

% typeset the table
\pgfplotstabletypeset
    [columns={TotalDistance,LocalDistance,DifferenceDistance}]
    {\mytable}

\end{document}

相關內容