Tips for data manipulation in Mathematica

G562 Geometric Morphometrics Tips for data manipulation in Mathematica Flatten[ ] - Takes a nested list and flattens it into a single list. Partitio...
Author: Brianne Gilbert
0 downloads 0 Views 950KB Size
G562 Geometric Morphometrics

Tips for data manipulation in Mathematica Flatten[ ] - Takes a nested list and flattens it into a single list.

Partition[ ] - Opposite of flatten: groups items into a nested list.

Transpose[ ] - Switches columns to rows and rows to columns.

Department of Geological Sciences | Indiana University

(c) 2012, P. David Polly

G562 Geometric Morphometrics

Taking rows and columns of data in Mathematica Example 1: take column 2 of row 5

Double square brackets after a variable allow you to select parts of a list or matrix. For these examples, data is a matrix with ten rows and seven columns. The first number in the brackets indicates rows, the second number indicates columns.

Mathematica code: data[[5, 2]] Example 2: take all of row 4

The code for taking the highlighted data is shown beneath each example.

Mathematica code: data[[4]] Department of Geological Sciences | Indiana University

(c) 2012, P. David Polly

G562 Geometric Morphometrics

Taking rows and columns (cont.) Example 3: take rows 3 to the last row

Example 5: take rows 3 then 6

Mathematica code: data[[3;;]]

Mathematica code: data[[{3,6}]]

Example 4: take rows 3 through 6

Example 6: take column 1

Mathematica code: data[[3;;6]]

Mathematica code: data[[1;;,1]]

Department of Geological Sciences | Indiana University

(c) 2012, P. David Polly

G562 Geometric Morphometrics

Taking rows and columns (cont.) Example 7: take columns 3 through the end

Example 9: take rows 4 through 8 in columns 3 through 5

Mathematica code: data[[1;;,3;;]]

Mathematica code: data[[1;;8,3;;5]]

Example 8: take columns 3 through 5

Mathematica code: data[[1;;,3;;5]]

Department of Geological Sciences | Indiana University

Example 10: take columns 3 then 6

Mathematica code: data[[1;;,{3,6}]]

(c) 2012, P. David Polly

G562 Geometric Morphometrics

Taking rows and columns (cont.) Example 11: take columns 6 then 3

Mathematica code: data[[1;;,{6,3}]]

Department of Geological Sciences | Indiana University

Example 12: take rows 1 - 6 of columns 2, 4, and 6

Mathematica code: data[[1;;6,{2,4,6}]]

(c) 2012, P. David Polly

G562 Geometric Morphometrics

Lists with more than two dimensions The examples so far assume that data are stored as a simple list or as a two-dimensional list (rows and columns). In principle, data can be stored in lists with three, four, or any number of dimensions. Consider an Excel workbook that has many worksheets, each with its own rows and columns - it is an example of a three dimensional array, which is what it becomes when it is imported into Mathematica. To retrieve the 3rd and 4th columns from all rows in the second “page” of a three dimensional array, the Mathematica code would be: data[[2, 1;;, {3,4}]] “2” applies to the first dimension of the array, “1;;” to its second dimension, and “{3,4}” to its third dimension. It is often helpful to build this up dimension by dimension: data[[2]] will show you everything in the second page so you can confirm you have the right thing data[[2, 1;;]] will show you all rows from one onward (which should be identical to the first step) data[[2, 1;;,{3,4}]] will limit the output to just columns 3 and 4.

Department of Geological Sciences | Indiana University

(c) 2012, P. David Polly

G562 Geometric Morphometrics

Map[] a function for repeating the same thing for every item in a list H* three ways to put your coordinates into the Point@D function *L coords Out[12]=

8848.425, 90.45