在 Mathematica 中读取此类文件

发布于 2024-09-30 16:45:20 字数 544 浏览 1 评论 0原文

这是数据文件:

ID  YR  MO  DA  YrM  MoM  DaM  
100  2010  2  20  2010  8  30  
110  2010  4  30  2010  9  12     
112  2010  8  20  2010  10  28  

我应该能够访问此文件中的每个元素,我尝试使用此函数在 Mathematica 中创建记录,但我收到错误

ReadList["testA.txt", Number, RecordLists -> true]

Error: ReadList::opttf: Value of option RecordLists -> true should be True or False.

另外,在完成记录后如何访问每个元素?

Mathematica 中还有一种方法可以再创建一个列,该列在两个日期之间存在差异并将其放入新列中。

这个作业确实允许使用 Excel 进行计算,但我必须在 Mathematica 中执行此操作。

This is the data file:

ID  YR  MO  DA  YrM  MoM  DaM  
100  2010  2  20  2010  8  30  
110  2010  4  30  2010  9  12     
112  2010  8  20  2010  10  28  

I should be able to access each element in this file, i tried to use this function in creating record in Mathematica but i am getting a error

ReadList["testA.txt", Number, RecordLists -> true]

Error: ReadList::opttf: Value of option RecordLists -> true should be True or False.

Also how do I access each element after doing the records?

Also is there a way in Mathematica to create one more column which does difference between two dates and put it in new column.

This homework assignment does allow to use excel to compute, but i have to do this in Mathematica.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

慕烟庭风 2024-10-07 16:45:20

您可以将 Import"Table 一起使用" 格式,甚至可以忽略标题行:

In[1:= Import["test.txt", "Table", "HeaderLines" -> 1]

Out[1]= {{100, 2010, 2, 20, 2010, 8, 30}, {110, 2010, 4, 30, 2010, 9,
12}, {112, 2010, 8, 20, 2010, 10, 28}}

You could instead use Import with the "Table" format, which can even ignore the header lines:

In[1:= Import["test.txt", "Table", "HeaderLines" -> 1]

Out[1]= {{100, 2010, 2, 20, 2010, 8, 30}, {110, 2010, 4, 30, 2010, 9,
12}, {112, 2010, 8, 20, 2010, 10, 28}}
装纯掩盖桑 2024-10-07 16:45:20

你问了3个问题,我会尽力回答所有问题。正如 belisarius 指出的,Mathematica 区分大小写。因此,您的代码应该是:

In[1]:=ReadList["testA.txt", Number, RecordLists -> True]

但是,这仍然会生成错误,因为您的第一行由 String 而不是 Number 组成。因此,最简单的方法就是采用 Michael Pilat 的解决方案 并使用导入。这将返回一个列表列表,其中文件中的每个记录都成为子列表之一。

要访问特定子列表,请使用 Part,或其更简单的形式[[ ]],如下所示:

In[2]:={{100, 2010, 2, 20, 2010, 8, 30}, 
        {110, 2010, 4, 30, 2010, 9,12}, 
        {112, 2010, 8, 20, 2010, 10, 28}}[[1]]
Out[2]:={100, 2010, 2, 20, 2010, 8, 30}

或者,如果您想要特定列

In[3]:={{100, 2010, 2, 20, 2010, 8, 30}, 
        {110, 2010, 4, 30, 2010, 9,12}, 
        {112, 2010, 8, 20, 2010, 10, 28}}[[All,4]]
Out[3]:={20, 30, 20}

现在,要将另一列添加到列表中,有几个的方式。最简单的方法是转置您的数据,

In[4]:=Transpose[data]
Out[4]:={{100, 110, 112}, {2010, 2010, 2010}, {2, 4, 8}, 
         {20, 30, 20}, {2010, 2010, 2010}, {8, 9, 10}, {30, 12, 28}}

选择现在的行并对它们应用函数,

In[5]:=Plus @@ Out[4][[{3,6}]]
Out[5]:={10,13,18}

将新行附加到旧数据,然后转回

In[6]:=Out[4]~Join~Out[5] // Transpose
Out[6]:={100, 2010, 2, 20, 2010, 8, 30, 10}, 
        {110, 2010, 4, 30, 2010, 9, 12, 13}, 
        {112, 2010, 8, 20, 2010, 10, 28, 18}}

概念上更困难,但更直接的方法是使用Map 将函数应用于原始数据中的每一行,返回包含新数据的行

In[7]:=Map[#~Join~{Plus@@#[[{3,6}]]}&, data]
Out[7]:={100, 2010, 2, 20, 2010, 8, 30, 10}, 
        {110, 2010, 4, 30, 2010, 9, 12, 13}, 
        {112, 2010, 8, 20, 2010, 10, 28, 18}}

You've asked 3 questions, and I'll try to answer them all. As belisarius pointed out, Mathematica is case sensitive. So, your code should be:

In[1]:=ReadList["testA.txt", Number, RecordLists -> True]

However, this will still generate an error as your first line is made up of Strings not Numbers. So, the simplest thing to do is to go with Michael Pilat's solution and use Import. This returns a list of lists where each record in the file becomes one of the sublists.

To access a specific sublist, you use Part, or its simpler form [[ ]], as follows:

In[2]:={{100, 2010, 2, 20, 2010, 8, 30}, 
        {110, 2010, 4, 30, 2010, 9,12}, 
        {112, 2010, 8, 20, 2010, 10, 28}}[[1]]
Out[2]:={100, 2010, 2, 20, 2010, 8, 30}

Or, if you want a specific column

In[3]:={{100, 2010, 2, 20, 2010, 8, 30}, 
        {110, 2010, 4, 30, 2010, 9,12}, 
        {112, 2010, 8, 20, 2010, 10, 28}}[[All,4]]
Out[3]:={20, 30, 20}

Now, to add another column to your list, there are a couple of ways. The simplest way is to Transpose your data,

In[4]:=Transpose[data]
Out[4]:={{100, 110, 112}, {2010, 2010, 2010}, {2, 4, 8}, 
         {20, 30, 20}, {2010, 2010, 2010}, {8, 9, 10}, {30, 12, 28}}

select the now rows and Apply the function to them,

In[5]:=Plus @@ Out[4][[{3,6}]]
Out[5]:={10,13,18}

attach the new row to the old data, and transpose back

In[6]:=Out[4]~Join~Out[5] // Transpose
Out[6]:={100, 2010, 2, 20, 2010, 8, 30, 10}, 
        {110, 2010, 4, 30, 2010, 9, 12, 13}, 
        {112, 2010, 8, 20, 2010, 10, 28, 18}}

A conceptually more difficult, but more straightforward method is to use Map to apply a function to each row in the original data that returns the row with the new datum present

In[7]:=Map[#~Join~{Plus@@#[[{3,6}]]}&, data]
Out[7]:={100, 2010, 2, 20, 2010, 8, 30, 10}, 
        {110, 2010, 4, 30, 2010, 9, 12, 13}, 
        {112, 2010, 8, 20, 2010, 10, 28, 18}}
话少情深 2024-10-07 16:45:20

正确正确:))

Mathematica 对Case-S敏感

True :))

Mathematica is Case-Sensitive

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