R Lavigne
R Lavigne

Reputation: 3

Manipulating Tables in Mathematica

The indexing scheme I'm trying seems so simple (but wrong)... data is the name of the table of data imported from an Oracle database. I tried moving pointer outside of List[....] and successfully tested (In[100]). I'm new to Mathematica... been going in little circles using Wolfram help tool.
Can someone get me out of rut and point me to a better method?

In[100]:= dat[n_, m_] := data[[n, m]];
          dat[1, 3]
Out[102]= 0.70388
In[104]:= List[dat[n, 3], {n, 1, Length[data]}]

During evaluation of In[104]:= Part::pkspec1: The expression n cannot be used as a part specification.

Out[104]= {{{0.00499, 0.00037, 0.70388, 0.00526, 0.00008, 
    0.00006}, {0.00176, 0.00006, 0.31315, 0.00129, 0.01142, 
    0.00016}, {0.00201, 0.00013, 0.32143, 0.00158, 0.01544, 
    0.00041}, {0.00507, 0.00015, 0.7228, 0.00263, 0.00002, 
    0.00001}, {0.00516, 0.00011, 0.72381, 0.0028, 0.00001, 
    0.00001}, {0.00203, 0.00019, 0.31207, 0.00297, 0.01143, 
    0.00044}, {0.00138, 0.00024, 0.26769, 0.00349, 0.00098, 
    0.00018}, {0.00182, 0.00007, 0.31365, 0.00129, 0.01383, 
    0.0002}, {0.00531, 0.00009, 0.72048, 0.00212, 0., 
    0.00001}, {0.00395, 0.00019, 0.60555, 0.00307, 0.00047, 
    0.00007}, {0.0055, 0.00017, 0.72021, 0.00332, 0.00001, 
    0.00001}, {0.00175, 0.00016, 0.30605, 0.00313, 0.00933, 
    0.00035}, {0.00198, 0.00009, 0.29497, 0.00135, 0.01019, 
    0.00023}, {0.00545, 0.00016, 0.72135, 0.00249, 0.00001, 
    0.00001}, {0.00179, 0.00038, 0.33179, 0.00791, 0.01627, 
    0.00097}, {0.00211, 0.00013, 0.31377, 0.00171, 0.01289, 
    0.00031}, {0.00515, 0.00057, 0.7213, 0.00766, 0.00006, 
    0.00018}, {0.00543, 0.00012, 0.72163, 0.0025, 0., 
    0.00001}, {0.00395, 0.00011, 0.72095, 0.00267, 0.00001, 
    0.00001}, {0.00548, 0.00014, 0.72343, 0.00278, 0., 0.00001}}[[n, 
  3]], {n, 1, 20}}

Upvotes: 0

Views: 818

Answers (1)

Sjoerd Smit
Sjoerd Smit

Reputation: 186

The List function is not meant to extract data in the way you're attempting to do here. List is basically just a wrapper that indicates that whatever is inside is a list of things. If you construct a list in Mathematica using the curly brackets ({...}), you're automatically calling the List function:

FullForm[{1, 2, 3}]

which gives:

List[1, 2, 3]

Always remember that in Mathematica you're invoking named functions, even if it's not immediately obvious. For example, if you type dat[[1]], you're actually invoking the Part function. You can see the functions you're using by wrapping your code in Hold (which tells Mathematica to just keep your code in its current form) and then in FullForm (which tells Mathematica to show you how Mathematica interprets your input internally):

FullForm[Hold[ dat[[1]] ]]

This results in:

Part[dat, 1]

So if you want to understand what's happening, read up on the Part function and any linked documentation pages in the Part documentation.

As for the rest of your question: if you want to have an index run over a range, you can use the Table function. This way your code should work:

Table[dat[n, 3], {n, 1, Length[data]}]

However, I wouldn't even bother with the definition you put on the top. You can access the elements of your data using Spans (look up that function in the documentation. Spans are abbreviated with the symbols ;;) and All like this:

data[[All, 3]]

or:

data[[1 ;; Length[data], 3]]

I would suggest reading these parts of the documentation (you can find them under the Mathematica documentation under F1 as well as using the following online links):

https://reference.wolfram.com/language/tutorial/ManipulatingElementsOfLists.html

https://reference.wolfram.com/language/ref/Part.html

https://reference.wolfram.com/language/ref/Span.html

https://reference.wolfram.com/language/ref/Take.html

https://reference.wolfram.com/language/ref/Table.html

Upvotes: 1

Related Questions