在 Mathematica 中处理表格
Manipulating Tables in Mathematica
我正在尝试的索引方案看起来很简单(但错误)...
data 是从 Oracle 数据库导入的 table 数据的名称。
我尝试将指针移到 List[....] 之外并成功测试(In[100])。
我是 Mathematica 的新手...一直在使用 Wolfram 帮助工具小圈子。
有人可以让我摆脱困境并指出更好的方法吗?
In[100]:= dat[n_, m_] := data[[n, m]];
dat[1, 3]
Out[102]= 0.70388
In[104]:= List[dat[n, 3], {n, 1, Length[data]}]
During evaluation of In[104]:= Part::pkspec1: The expression n cannot be used as a part specification.
Out[104]= {{{0.00499, 0.00037, 0.70388, 0.00526, 0.00008,
0.00006}, {0.00176, 0.00006, 0.31315, 0.00129, 0.01142,
0.00016}, {0.00201, 0.00013, 0.32143, 0.00158, 0.01544,
0.00041}, {0.00507, 0.00015, 0.7228, 0.00263, 0.00002,
0.00001}, {0.00516, 0.00011, 0.72381, 0.0028, 0.00001,
0.00001}, {0.00203, 0.00019, 0.31207, 0.00297, 0.01143,
0.00044}, {0.00138, 0.00024, 0.26769, 0.00349, 0.00098,
0.00018}, {0.00182, 0.00007, 0.31365, 0.00129, 0.01383,
0.0002}, {0.00531, 0.00009, 0.72048, 0.00212, 0.,
0.00001}, {0.00395, 0.00019, 0.60555, 0.00307, 0.00047,
0.00007}, {0.0055, 0.00017, 0.72021, 0.00332, 0.00001,
0.00001}, {0.00175, 0.00016, 0.30605, 0.00313, 0.00933,
0.00035}, {0.00198, 0.00009, 0.29497, 0.00135, 0.01019,
0.00023}, {0.00545, 0.00016, 0.72135, 0.00249, 0.00001,
0.00001}, {0.00179, 0.00038, 0.33179, 0.00791, 0.01627,
0.00097}, {0.00211, 0.00013, 0.31377, 0.00171, 0.01289,
0.00031}, {0.00515, 0.00057, 0.7213, 0.00766, 0.00006,
0.00018}, {0.00543, 0.00012, 0.72163, 0.0025, 0.,
0.00001}, {0.00395, 0.00011, 0.72095, 0.00267, 0.00001,
0.00001}, {0.00548, 0.00014, 0.72343, 0.00278, 0., 0.00001}}[[n,
3]], {n, 1, 20}}
List
函数并不意味着按照您在此处尝试执行的方式提取数据。 List
基本上只是一个包装器,表示里面的内容都是一个列表。如果您在 Mathematica 中使用大括号 ({...}
) 构建列表,您将自动调用 List
函数:
FullForm[{1, 2, 3}]
给出:
List[1, 2, 3]
永远记住,在 Mathematica 中您正在调用命名函数,即使它不是很明显。例如,如果您键入 dat[[1]]
,您实际上是在调用 Part
函数。您可以通过将代码包装在 Hold
中(它告诉 Mathematica 将您的代码保持在当前形式)然后在 FullForm
中(它告诉 Mathematica 向您展示 Mathematica 如何在内部解释您的输入):
FullForm[Hold[ dat[[1]] ]]
这导致:
Part[dat, 1]
因此,如果您想了解发生了什么,请阅读 Part
函数和 Part
文档中的所有链接文档页面。
至于你的问题的其余部分:如果你想要一个范围内的索引 运行,你可以使用 Table
函数。这样你的代码应该可以工作:
Table[dat[n, 3], {n, 1, Length[data]}]
但是,我什至不会理会你放在最上面的定义。您可以使用 Span
s(在文档中查找该函数。Span
s 缩写为符号 ;;
)和 All
来访问数据元素,如下所示:
data[[All, 3]]
或:
data[[1 ;; Length[data], 3]]
我建议阅读文档的这些部分(您可以在 F1 下的 Mathematica 文档下找到它们,也可以使用以下在线链接):
https://reference.wolfram.com/language/tutorial/ManipulatingElementsOfLists.html
https://reference.wolfram.com/language/ref/Part.html
https://reference.wolfram.com/language/ref/Span.html
我正在尝试的索引方案看起来很简单(但错误)...
data 是从 Oracle 数据库导入的 table 数据的名称。
我尝试将指针移到 List[....] 之外并成功测试(In[100])。
我是 Mathematica 的新手...一直在使用 Wolfram 帮助工具小圈子。
有人可以让我摆脱困境并指出更好的方法吗?
In[100]:= dat[n_, m_] := data[[n, m]];
dat[1, 3]
Out[102]= 0.70388
In[104]:= List[dat[n, 3], {n, 1, Length[data]}]
During evaluation of In[104]:= Part::pkspec1: The expression n cannot be used as a part specification.
Out[104]= {{{0.00499, 0.00037, 0.70388, 0.00526, 0.00008,
0.00006}, {0.00176, 0.00006, 0.31315, 0.00129, 0.01142,
0.00016}, {0.00201, 0.00013, 0.32143, 0.00158, 0.01544,
0.00041}, {0.00507, 0.00015, 0.7228, 0.00263, 0.00002,
0.00001}, {0.00516, 0.00011, 0.72381, 0.0028, 0.00001,
0.00001}, {0.00203, 0.00019, 0.31207, 0.00297, 0.01143,
0.00044}, {0.00138, 0.00024, 0.26769, 0.00349, 0.00098,
0.00018}, {0.00182, 0.00007, 0.31365, 0.00129, 0.01383,
0.0002}, {0.00531, 0.00009, 0.72048, 0.00212, 0.,
0.00001}, {0.00395, 0.00019, 0.60555, 0.00307, 0.00047,
0.00007}, {0.0055, 0.00017, 0.72021, 0.00332, 0.00001,
0.00001}, {0.00175, 0.00016, 0.30605, 0.00313, 0.00933,
0.00035}, {0.00198, 0.00009, 0.29497, 0.00135, 0.01019,
0.00023}, {0.00545, 0.00016, 0.72135, 0.00249, 0.00001,
0.00001}, {0.00179, 0.00038, 0.33179, 0.00791, 0.01627,
0.00097}, {0.00211, 0.00013, 0.31377, 0.00171, 0.01289,
0.00031}, {0.00515, 0.00057, 0.7213, 0.00766, 0.00006,
0.00018}, {0.00543, 0.00012, 0.72163, 0.0025, 0.,
0.00001}, {0.00395, 0.00011, 0.72095, 0.00267, 0.00001,
0.00001}, {0.00548, 0.00014, 0.72343, 0.00278, 0., 0.00001}}[[n,
3]], {n, 1, 20}}
List
函数并不意味着按照您在此处尝试执行的方式提取数据。 List
基本上只是一个包装器,表示里面的内容都是一个列表。如果您在 Mathematica 中使用大括号 ({...}
) 构建列表,您将自动调用 List
函数:
FullForm[{1, 2, 3}]
给出:
List[1, 2, 3]
永远记住,在 Mathematica 中您正在调用命名函数,即使它不是很明显。例如,如果您键入 dat[[1]]
,您实际上是在调用 Part
函数。您可以通过将代码包装在 Hold
中(它告诉 Mathematica 将您的代码保持在当前形式)然后在 FullForm
中(它告诉 Mathematica 向您展示 Mathematica 如何在内部解释您的输入):
FullForm[Hold[ dat[[1]] ]]
这导致:
Part[dat, 1]
因此,如果您想了解发生了什么,请阅读 Part
函数和 Part
文档中的所有链接文档页面。
至于你的问题的其余部分:如果你想要一个范围内的索引 运行,你可以使用 Table
函数。这样你的代码应该可以工作:
Table[dat[n, 3], {n, 1, Length[data]}]
但是,我什至不会理会你放在最上面的定义。您可以使用 Span
s(在文档中查找该函数。Span
s 缩写为符号 ;;
)和 All
来访问数据元素,如下所示:
data[[All, 3]]
或:
data[[1 ;; Length[data], 3]]
我建议阅读文档的这些部分(您可以在 F1 下的 Mathematica 文档下找到它们,也可以使用以下在线链接):
https://reference.wolfram.com/language/tutorial/ManipulatingElementsOfLists.html
https://reference.wolfram.com/language/ref/Part.html
https://reference.wolfram.com/language/ref/Span.html