Mathematica “AppendTo"功能问题 [英] Mathematica "AppendTo" function problem

查看:59
本文介绍了Mathematica “AppendTo"功能问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Mathematica 的新手,在向数据表中添加列时遇到了重大故障.我在 Vista 中运行 Mathematica 7.在这里问之前,我已经花了很多时间 RFD.

I'm a newbie in Mathematica and I'm having a major malfunction with adding columns to a data table. I'm running Mathematica 7 in Vista. I have spent a lot of time RFD before asking here.

我有一个包含三列五行的数据表 (mydata).我正在尝试向表中添加两个包含五个元素的列表(有效地向数据表中添加了两列).

I have a data table (mydata) with three columns and five rows. I'm trying to add two lists of five elements to the table (effectively adding two columns to the data table).

完美:

Table[AppendTo[mydata[[i]],myfirstlist[[i]]],{i,4}] 

使用以下内容打印表格:mydata//TableForm 显示添加的列.

Printing out the table with: mydata // TableForm shows the added column.

但是,当我尝试添加第二个列表时

However, when I try to add my second list

Table[AppendTo[mydata[[i]],mysecondlist[[i]]],{i,5}]

要么 Mathematica 崩溃(!),要么我收到大量 Part::partwPart::spec 错误,说第 5 部分不存在.

either Mathematica crashes(!) or I get a slew of Part::partw and Part::spec errors saying Part 5 does not exist.

然而,在所有的错误信息之后(如果 Mathematica 没有崩溃),再次打印出数据表:mydata//TableForm 就像我想要的那样显示了有五列的数据表.修改后的数据表上的所有 TableForm 格式选项都可以正常工作.

However, after all the error messages (if Mathematica does not crash), again printing out the data table with: mydata // TableForm shows the data table with five columns just like I intended. All TableForm formatting options on the revised data table work fine.

谁能告诉我我做错了什么?提前致谢!

Could anyone tell me what I'm doing wrong? Thanks in advance!

推荐答案

让我们尝试阐明双转置方法的组成.我对这种方法的独创性不做任何声明.我的重点是阐述的清晰度.

Let's try to clarify what the double transpose method consists of. I make no claims about the originality of the approach. My focus is on the clarity of exposition.

让我们从 5 个列表开始.首先我们将三个放在一张桌子上.然后我们将添加最后两个.

Let's begin with 5 lists. First we'll place three in a table. Then we'll add the final two.

food = {"bagels", "lox", "cream cheese", "coffee", "blueberries"};
mammals = {"fisher cat", "weasel", "skunk", "raccon", "squirrel"};
painters = {"Picasso", "Rembrandt", "Klee", "Rousseau", "Warhol"};
countries = {"Brazil", "Portugal", "Azores", "Guinea Bissau", 
             "Cape Verde"};
sports = {"golf", "badminton", "football", "tennis", "rugby"};

前三个列表——食物、哺乳动物、画家——成为lists3的元素.它们只是列表,但 TableForm 在表格中将它们显示为.

The first three lists--food, mammals, painters--become the elements of lists3. They are just lists, but TableForm displays them in a table as rows.

(lists3 = {food, mammals, painters}) // TableForm

mydata 将是 lists3 转置的名称.现在这三个列表显示为.这就是换位的作用:列和行互换.

mydata will be the name for lists3 transposed. Now the three lists appear as columns. That's what transposition does: columns and rows are switched.

(mydata = Transpose@lists3) // TableForm

这才是问题真正开始的地方.我们如何添加两个额外的列(即国家和体育列表)?那么让我们处理剩下的两个列表.

This is where the problem actually begins. How can we add two additional columns (that is, the lists for countries and sports)? So let's work with the remaining two lists.

(lists2 = {countries, sports}) // TableForm

所以我们可以加入 Transpose[mydata]lists2....

So we can Join Transpose[mydata] and lists2....

(lists5 = Join[Transpose[mydata], lists2]) // TableForm

[或者,我们可能有 Joined lists3lists2 因为第二个转置,mydata 撤销先前的移调.lists3 只是mydata 的转置.(反之亦然).]

[Alternatively, we might have Joined lists3 and lists2 because the second transposition, the transposition of mydata undoes the earlier transposition. lists3 is just the transposition of mydata. (and vice-versa).]

In[]:= lists3 === Transpose[mydata]
Out[]:= True

现在我们只需要对结果进行转置即可获得所需的五个列表的最终表,每个列表占据自己的一列:

Now we only need to Transpose the result to obtain the desired final table of five lists, each occupying its own column:

Transpose@lists5 // TableForm

我希望这有助于阐明如何向表中添加两列.我觉得这种方式相当清楚.您可能会发现其他更清晰的方法.

I hope that helps shed some light on how to add two columns to a table. I find this way reasonably clear. You may find some other way clearer.

这篇关于Mathematica “AppendTo"功能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆