# Table/Tree of values

My question:

Is there a way to create a tree of values? Something like the output of the command TreeForm, but with values in the nodes?

Why do I want this?

I'm trying to do a complete program to analyse the output of my labs classes. Each column of data has a symbol assigned. In general, each column is meaningful: it's not just a pile of diferent variables. What i want to say is that in general,calculations are done "column wise".My problem is when i need a to do a calculation that needs a more envolved "horziontal" structure : Assigning the variables to columns lacks "horizontal flexibility" . (In a way,this is the kind of problems that are solved in Excel with the  and array formulas)

let me ilustrate with an example:

y={1,2,3,4,5,6,7,8,9};
x={-1,-2,-3};


I want to associate the 1;;3, 4;;8, 9;;9 parts of y to each element of x. What i mean by associating is that for some calculation the input of the function will have as argument each of this sets.

I'm aware of functions like Map, Apply, Thread and MapThread. I've been using them to solve this kind of problems, but sometimes it gets a little confusing.

I'm also aware of Partition, wich would solve my problem if i wanted to separate y in subarrays of the same length.

As i say in my question, what i want is to construct something like a net/tree that "archives" the structure of the arguments in each step of my calculations. Something like in networking theorys, when each node as an associated list of it's connections to the rest of the network. Notice that this list should not contains the values but some kind of coordinates of the connected nodes

Example: Calculate the mean and the mean square deviation of the irregular partition of lenghts n={3,2,5} of the list

 y={3,5,8,7,9,4,6,2,1,5};


My very conceptual aproach:

The first column of my Table/Tree will be the data y. To refer to some value on some column, I will use a pair of coordinates i,j: i stands for the column and j stands for the internal position. I will assign to y the coordinate i=1.

For the means calculation, what kind of "calculating conections" i have?

Yav=F1[y]=Mean[y]


The column of the means, Xav i=2, will have 3 elements. To each one I assign a list of conections to y:

(Conection of "" is rerpesent with a C"")

    CYav[[1]]: {1,{1,2,3}}
CYav[[2]]: {1,{4,5}};
CYav[[3]]: {1,{6,7,8,9,10}}


The connection are written in the form {i,{j's of the elements of i}}

Now, let's calculate the mean squared deviation. That is ,

   Ymsd=F2[y,Yav]=Mean[(y-Yav)^2]


This column as i=3 and also 3 elements.

For this calculation, i want to use the columns i=1,2. The calculating connections to y are the same that the ones used to calculate Yav. But now, i need to connect Ymsd to Yav.

    CYmsd[[1]]: {{1,{1,2,3}},{2,1}}
CYmsd[[2]]: {{1,{4,5}},{2,2}}
CYmsd[[3]]: {{1,{6,7,8,9,10}},{2,3}}


Now the conections are a pair of conections of the former type, one to each column connected.

After assigning the conections, i would use a function that would fetch the correct values, guided by the map created, and apply the F1,F2.

Thanks

• I'm not sure I understand why you are doing this. Wouldn't a far easier data structure use something like y[[1]]={1,2,3}, y[[2]]={4,5}, y[[3]]={6,7,8,9} instead of the flattened list? This more easily mimics the "two-dimensional structure of Excel" and makes all your succeeding calculations very straightforward. – bill s Apr 21 '13 at 11:06
• bill, i want to leave y as a flattened list, because i want to do different calculations that agroup the y elements in different ways. For example, imagine that after do this calculation,i want to calculate the means of the regular partition of y in pieces of length 3. – João Cortes Apr 21 '13 at 11:48

## 1 Answer

To Solve your example:

Calculate the mean and the mean square deviation of the irregular partition of lengths n={3,2,5} of the list y={3,5,8,7,9,4,6,2,1,5};

(* irregular partitions of lists based on Nest *)
iPartition[x_, lengths_] :=
First@Nest[{Append[First@#, Take[#[[2]], First@Last@#]],
Drop[#[[2]], First@Last@#], Rest@Last@#} &, {{}, x, lengths},
Length@lengths]


Or

 (* irregular partitions of lists based on Accumulate *)
iPartition[x_, lengths_] :=
Take[x, #] & /@ (Transpose[{Most@(Prepend[#, 0] + 1), #} &@Accumulate@lengths])


Now calculate the means and standard deviations

{Mean@#, StandardDeviation@#} & /@ iPartition[y, n] // N


{{5.33333, 2.51661}, {4., 1.41421}, {3.2, 1.92354}}

• Thank you. Different from what I expected, but very useful – João Cortes Apr 21 '13 at 13:22
• Cheers, I hope it gets you closer to a solution, see edit for a correction to the Nest form of iPartition. – image_doctor Apr 21 '13 at 14:41