如何在 Mathematica 中将文件中的数据作为数组导入?
我正在尝试将数据集导入 Mathematica。问题是我无法将导入的数据作为 Mathematica 中的常规数字。它们作为“列表”导入。因此,我无法在我需要的方程中使用它们:
Import["out.dat", "Data"]
{{5.7, 4.3}, {-1.2, 7.8}}
Array[cc, {2, 2}]
For[i = 1, i <= 2, i++,
For[j = 1, j <= 2, j++,
cc[i, j] = Take[Import["out.dat", {"Data", i}], {j, j}]]]
现在,我需要 c[1,1]
为 5.7
但它是 {5.7} 如您所见:
cc[1, 1]
{5.7}
I am trying to import a data set to Mathematica. The problem is that I cannot have the imported data as regular numbers in Mathematica. They are imported as "list". So, I cannot use them in the equations I need:
Import["out.dat", "Data"]
{{5.7, 4.3}, {-1.2, 7.8}}
Array[cc, {2, 2}]
For[i = 1, i <= 2, i++,
For[j = 1, j <= 2, j++,
cc[i, j] = Take[Import["out.dat", {"Data", i}], {j, j}]]]
Now, I need c[1,1]
to be 5.7
but it is {5.7}
as you see:
cc[1, 1]
{5.7}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先要访问数组元素,请使用“[[ ]]”
更新。
c[[1,1]] 用于访问 2
要访问的维数组(矩阵)
简单数组使用 c[[1]]
更新 2.
如果 cc[1, 1] == {5.7} 则再次使用数组元素选择器:
Firstly to access an array element use "[[ ]]"
Update.
c[[1,1]] used to access to 2
dimensional array (matrix) to access
simple array use c[[1]]
Update 2.
If
cc[1, 1] == {5.7}
then use array element selector again:这确实取决于 dat 文件内容的精确格式。例如,如果文件仅包含数字制表符(或空格)和行分隔符,如下所示:
然后该语句
将数据直接加载到变量 cc 中。然后,使用正确的数组下标表示法,即 [[ 和 ]] 而不是 [ 和 ],您可以根据需要访问数组每个元素中的数字。这非常非常简单。如果您的输入文件更复杂,您应该 (a) 简化它,或 (b) 研究 Import[] 函数的各种选项和参数。
一般来说,如果您发现自己在 Mathematica 中编写循环,那么您就做得不对。
该形式
对于 Mathematica 来说, 是一个函数定义。它看起来非常像对数组中元素的赋值,这意味着您可以定义各种有趣的对象,这些对象是函数但看起来像数组,或者数组看起来像函数。当然,这是因为数组是从索引空间到元素集的函数。但对于 Mathematica,[ 和 ] 分隔函数的参数。
根据您的问题和评论,我怀疑您是 Mathematica 的初学者。在线文档非常好,但是您必须阅读它才能从中获得任何价值。
This does depend on the precise format of the contents of your dat file. For example, if the file contains just numbers tab (or space) and line separated like this:
Then the statement
loads the data directly into the variable cc. Then, using the correct notation for array subscripting, i.e., [[ and ]] not [ and ] you can access the numbers in each element of the array as you wish. It's very very simple. If your input file is more complex you should either (a) simplify it, or (b) study the various options and parameters for the Import[] function.
As a general rule if you find yourself writing loops in Mathematica you are not doing it right.
The form
is, to Mathematica, a function definition. It looks very much like an assignment to an element in an array, which means that you can define all sorts of interesting objects which are functions but look like arrays, or arrays which look like functions. Of course, this is because an array is a function from index space to the set of elements. But to Mathematica [ and ] delimit arguments to a function.
Based on your question and your comments I suspect that you are a beginner with Mathematica. The on-line documentation is very good, but you have to read it to get any value from it.
我认为这符合您的要求:
然后 c[1, 1] 返回 5.7(例如)。
I think this does what you want:
Then c[1, 1] returns 5.7 (for example).