Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mathematica "AppendTo" function problem

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.

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).

This works perfectly:

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

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}]

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

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!

like image 951
user656058 Avatar asked Dec 16 '22 16:12

user656058


2 Answers

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.

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"};

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

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

mydata

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

lists2

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

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

lists5

[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

final table

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.

like image 93
DavidC Avatar answered Dec 31 '22 23:12

DavidC


There are several things to cover here. First, the following code does not give me any errors, so there may be something else going on here. Perhaps you should post a full code block that produces the error.

mydata = Array[Subscript[{##}] &, {5, 3}];
myfirstlist = Range[1, 5];
mysecondlist = Range[6, 10];

Table[AppendTo[mydata[[i]], myfirstlist[[i]]], {i, 4}];
mydata // TableForm

Table[AppendTo[mydata[[i]], mysecondlist[[i]]], {i, 5}];
mydata // TableForm

Second, there is no purpose in using Table here, as you are modifying mydata directly. Table will use up memory pointlessly.

Third, there are better ways to accomplish this task.

See How to prepend a column and Inserting into a 2d list

I must retract my definitive statement that there are better ways. After changing Table to Do and running a few quick tests, this appears to be a competitive method for some data.

I am using Mathematica 7, so that does not appear to be the problem.

like image 30
Mr.Wizard Avatar answered Dec 31 '22 22:12

Mr.Wizard