RE: Nested Lists

• To: mathgroup at smc.vnet.net
• Subject: [mg28389] RE: [mg28373] Nested Lists
• From: "David Park" <djmp at earthlink.net>
• Date: Sun, 15 Apr 2001 00:13:40 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```Bob,

Here is some sample data with N = 3.

data = Table[{t, {{Cos[t], Sin[t], 0},
{0, Cos[t], Sin[t]}, {t, t^2/25, t^3/125}}},
{t, 0, 5, 0.05}];

As an example, the 5'th piece of data is

data[[5]]
{0.2, {{0.980067, 0.198669, 0}, {0, 0.980067, 0.198669}, {0.2, 0.0016,
0.000064}}}

This is a routine that will extract the data associated with the n'th
object. It is suitable for routines like Fit, but not for plotting.

extractdata[n_] := ({#1[[1]], #1[[2,n]]} & ) /@ data

data1 = extractdata[1];
data3 = extractdata[3];

Here is the 5'th piece of data1

data1[[5]]
{0.2, {0.980067, 0.198669, 0}}

This form of the data is suitable for routines like Fit. Notice that Fit
works on vector functions although the documentation doesn't explicitly
mention it.

Fit[data1, {Sin[t], Cos[t], t, t^2, t^3}, t] // Chop
{1. Cos[t], 1. Sin[t], 0}

Fit[data3, {Sin[t], Cos[t], t, t^2, t^3}, t] // Chop
{1. t, 0.04 t^2, 0.008 t^3}

On the other hand, if we want to plot the points with a routine like
ScatterPlot3D, you can extract the points this way:

Needs["Graphics`Graphics3D`"]
plotdata3 = (#1[[2,3]] & ) /@ data;
ScatterPlot3D[plotdata3]

David Park

> From: Robert Love [mailto:rlove at neosoft.com]
To: mathgroup at smc.vnet.net
> I'm confused about nested lists and how to select/extract data from them.
>
> I have a file of data I can use ReadList to read.  The data is laid out
> like this:
>
> t1
> x y z
> x y z
> x y z
> x y z
> t2
> x y z
> x y z
> x y z
> x y z
> t3
> etc.
>
> The data consists of a timestamp on a line and then the 3D position of
> each of the N objects for that time.  N is always the same number
> so I read it it like this:
>
>
> I'm doing this from memory since the computer w/Mathematica is at work.
> So I may not have every  detail of the ReadList correct but what I end up
> with is
>
> {{t1,{{x,y,z},{x,y,z},{x,y,z},{x,y,z}},
>  {t2,{{x,y,z}...
>
>
> My question is now  do I extract this  suitable for graphing, that is, how
> do I turn this into something like a set of all the object #4 positions
> along with the time?  I'm sure there is some combination of Flatten, Take,
> Transpose and Map that  will do what I want.  What is it?  Also, is there
> a better way to use  ReadList?  I'm stuck with the file format but I can
> change how I read the data.
>