我想在二维数组中存储一些计算值。但是,例如对于 2 对 2 矩阵,我得到的 [1,2] 和 [2,1] 值相同。我做错了什么?
\documentclass[11pt]{scrartcl}
\usepackage[utf8x]{inputenc}
\usepackage{fp} % fp-package
\usepackage{forloop,arrayjobx}
\begin{document}
\newcommand{\CreateArray}[2]{
\FPeval\maxi{1+#1} \FPeval\maxj{1+#2}
\FPclip\maxi{\maxi} \FPclip\maxj{\maxj}
\newcounter{i} \newcounter{j}
\newarray\MatrixA
\expandarrayelementtrue
When I set up the matrix everything works fine:
\forloop{i}{1}{\thei < \maxi}{ % Rows
\forloop{j}{1}{\thej < \maxj}{ % Columns
\FPeval\calcvar{\thej+100*\thei} % use variables i and j for calculation
\FPclip\calcvar{\calcvar}
\MatrixA(\thei,\thej)={\calcvar} % save \calcvar value in matrix
\FPprint{\thei} - \FPprint{\thej} - \FPprint{\calcvar} \\
}
}
But once I want to se the values stored the fields [2,1] and [1,2] are identical:
\forloop{i}{1}{\thei < \maxi}{ % Rows
\forloop{j}{1}{\thej < \maxj}{ % Columns
\thei - \thej - \MatrixA(\thei,\thej) \\
}
}
}
\CreateArray{2}{2} % crete matrix
\end{document}
答案1
我已经找到了解决方法,但我仍然对您关于解决初始问题的建议感兴趣。我发现列表工作得很好,所以我决定使用一个长列表,而对于每个“i”,我只需增加 1000 个条目,然后开始保存“i*1000+j”处的条目。这甚至可能适用于多维数组,具体取决于一个列表可能具有的最大长度。但这仍然不是我想要的解决方案,希望您能帮助我获得更好的代码。
\documentclass[11pt]{scrartcl}
\usepackage[utf8x]{inputenc}
\usepackage{fp} % fp-package
\usepackage{forloop,arrayjobx}
\begin{document}
\newcommand{\CreateArray}[2]{
\FPset\jump{1000} % array as long list, max \jump values per column
\FPeval\maxi{1+#1} \FPclip\maxi{\maxi}
\FPeval\maxj{1+#2} \FPclip\maxj{\maxj}
\newcounter{i} \newcounter{j} \newarray\MatrixA
\expandarrayelementtrue
\forloop{i}{1}{\thei < \maxi}{ % Rows
\forloop{j}{1}{\thej < \maxj}{ % Columns
\FPeval\iloc{1+\thei*\jump-\jump} \FPclip\iloc{\iloc}
\FPeval\calcvar{\thej+100*\thei}
\FPclip\calcvar{\calcvar}
\MatrixA(\iloc,\thej)={\calcvar} % save rsults in MatrixA Matrix
% \FPprint{\thei} - \FPprint{\thej} -
\FPprint{\calcvar} \\ % Testausgabe
}
}
\forloop{i}{1}{\thei < \maxi}{ % Rows
\forloop{j}{1}{\thej < \maxj}{ % Columns
\FPeval\iloc{1+\thei*\jump-\jump} \FPclip\iloc{\iloc}
\thei - \thej - \MatrixA(\iloc,\thej) \\
}
}
}
\CreateArray{3}{3} % Matrix with {rows}{columns}
\MatrixA(1,1) \MatrixA(1,2) \MatrixA(1,3) \\
\MatrixA(1001,1) \MatrixA(1001,2) \MatrixA(1001,3) \\
\MatrixA(2001,1) \MatrixA(2001,2) \MatrixA(2001,3)
\begin{tabular}{ccc}
\forloop{i}{1}{\thei < \maxi}{ % Rows
\forloop{j}{1}{\thej < \maxj}{ % Columns
\FPeval\iloc{1+\thei*\jump-\jump} \FPclip\iloc{\iloc}
\MatrixA(\iloc,\thej)
}
\\
}
\end{tabular}
\end{document}
答案2
您缺少\dataheight=2
(对应于),它需要插入到执行初始分配的\maxj
嵌套 s 之前。forloop
为了清楚起见,修改以下部分,添加\dataheight=2
When I set up the matrix everything works fine:
\dataheight=2
\forloop{i}{1}{\thei < \maxi}{ % Rows
\forloop{j}{1}{\thej < \maxj}{ % Columns
\FPeval\calcvar{\thej+100*\thei} % use variables i and j for calculation
\FPclip\calcvar{\calcvar}
\MatrixA(\thei,\thej)={\calcvar} % save \calcvar value in matrix
\FPprint{\thei} - \FPprint{\thej} - \FPprint{\calcvar} \\
}
}
我遇到了非常类似的问题(“为什么 arrayjobx 返回我定义的二维矩阵的错误元素?“) 和埃格尔按照我给你的相同建议解决了这个问题。