如何在 Mathematica 中将文件中的数据作为数组导入?

发布于 2024-08-19 03:43:13 字数 441 浏览 4 评论 0原文

我正在尝试将数据集导入 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌逼全场 2024-08-26 03:43:13

首先要访问数组元素,请使用“[[ ]]”

    c = {{5.7, 4.3}, {-1.2, 7.8}};
    c[[1, 1]]
    Out=5.7

更新。

c[[1,1]] 用于访问 2
要访问的维数组(矩阵)
简单数组使用 c[[1]]

    In[27]:= Import["d:\\dat.out", "Table"]    
    Out[27]= {{5.7, 4.3, -1.2, 7.8}}    
    In[28]:= %[[1]]    
    Out[28]= {5.7, 4.3, -1.2, 7.8}    
    In[29]:= IntegerPart[#] & /@ %    
    Out[29]= {5, 4, -1, 7} 
etc...

更新 2.

如果 cc[1, 1] == {5.7} 则再次使用数组元素选择器:

cc[1,1][[1]]

Firstly to access an array element use "[[ ]]"

    c = {{5.7, 4.3}, {-1.2, 7.8}};
    c[[1, 1]]
    Out=5.7

Update.

c[[1,1]] used to access to 2
dimensional array (matrix) to access
simple array use c[[1]]

    In[27]:= Import["d:\\dat.out", "Table"]    
    Out[27]= {{5.7, 4.3, -1.2, 7.8}}    
    In[28]:= %[[1]]    
    Out[28]= {5.7, 4.3, -1.2, 7.8}    
    In[29]:= IntegerPart[#] & /@ %    
    Out[29]= {5, 4, -1, 7} 
etc...

Update 2.

If cc[1, 1] == {5.7} then use array element selector again:

cc[1,1][[1]]
太阳哥哥 2024-08-26 03:43:13

这确实取决于 dat 文件内容的精确格式。例如,如果文件仅包含数字制表符(或空格)和行分隔符,如下所示:

5.7   4.3
-1.2  7.8

然后该语句

cc = Import["out.dat"]

将数据直接加载到变量 cc 中。然后,使用正确的数组下标表示法,即 [[ 和 ]] 而不是 [ 和 ],您可以根据需要访问数组每个元素中的数字。这非常非常简单。如果您的输入文件更复杂,您应该 (a) 简化它,或 (b) 研究 Import[] 函数的各种选项和参数。

一般来说,如果您发现自己在 Mathematica 中编写循环,那么您就做得不对。

该形式

c[1,1] = 5.7

对于 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:

5.7   4.3
-1.2  7.8

Then the statement

cc = Import["out.dat"]

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

c[1,1] = 5.7

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.

风向决定发型 2024-08-26 03:43:13

我认为这符合您的要求:

mat = {{5.7, 4.3}, {-1.2, 7.8}};
Do[c[i, j] = mat[[i, j]], {i, 2}, {j, 2}]

然后 c[1, 1] 返回 5.7(例如)。

I think this does what you want:

mat = {{5.7, 4.3}, {-1.2, 7.8}};
Do[c[i, j] = mat[[i, j]], {i, 2}, {j, 2}]

Then c[1, 1] returns 5.7 (for example).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文