Re: structure array equivalent in Mathematica
- To: mathgroup at smc.vnet.net
- Subject: [mg67229] Re: structure array equivalent in Mathematica
- From: Rolf.Mertig at gmail.com
- Date: Tue, 13 Jun 2006 01:07:30 -0400 (EDT)
- References: <e6gdun$ngj$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Here are two possibilities to do what you want: (* by using a datatype identitifier the order of data can be arbitrary: *) observation = { DataType["pressure"][10.], DataType["temperature"][20.], DataType["water_vapor"][30.] } (* one possibility to define the "method" - functions : *) pressure = Function[x,Cases[x,DataType["pressure"][_?NumberQ],-1]/.DataType[_][y_]:>y/.{z_}:>z]; temperature = Function[x,Cases[x,DataType["temperature"][_?NumberQ],-1]/.DataType[_][y_]:>y/.{z_}:>z]; watervapor = Function[x,Cases[x,DataType["water_vapor"][_?NumberQ],-1]/.DataType[_][y_]:>y/.{z_}:>z]; { pressure @ observation , temperature @ observation , watervapor @ observation } data["obs_day"] = {observation, observation, observation}; pressure @ data["obs_day"][[1]] (* this will also work, by design: *) pressure @ data["obs_day"] (* ********************** *) (* for a lot of data it is more efficient to represent the different data types by position only: *) observation2 = {10., 20., 30.}; { pressure2 = Function[obs, obs[[1]]]; , temperature2 = Function[obs, obs[[2]]]; , watervapor2 = Function[obs, obs[[3]]]; } pressure2 @ observation2 temperature2 @ observation2 watervapor2 @ observation2 data2["obs_day"] = {observation2, observation2, observation2}; pressure2 @ data2["obs_day"][[1]] --- Regards, Rolf Mertig http://www.gluonvision.com GluonVision GmbH Berlin kevin_jazz wrote: > Like many people I imagine, I'm transitioning to Mathematica from a > background in another system. > One of the common data types is the structure array. Let's say I have > an observational data set that includes pressure, temperature, and > water vapor as a function of altitude. So, in pseudo-code I might > define a structure as > > observation = {pressure: float(100), temperature: float(100), > water_vapor: float(100)} > > I could then access the elements of this observation as > > observation.pressure > observation.temperature, etc. > > Furthermore, I could aggregate these observations into a larger list, e.g. > obs_day = {observation, observation, observation} > to be accessed as > obs_day[1].pressure for the first element (assuming 1-index). > > Now, the list in Mathematica is quite powerful and I think can be > set-up in a similar fashion. > > So my question is how is the structure array commonly implemented in > Mathematica or its equivalent? > > If there is a previous thread (I looked but didn't find any) on the > topic or in the Mathematica book or Mathematica Journal that I missed, > feel free to point me in that direction. > > Many thanks, > > Kevin Bowman > Jet Propulsion Laboratory