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:


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


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?


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 ,


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.


  • $\begingroup$ 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. $\endgroup$
    – bill s
    Apr 21, 2013 at 11:06
  • $\begingroup$ 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. $\endgroup$ Apr 21, 2013 at 11:48

1 Answer 1


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}, 


 (* 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}}

  • $\begingroup$ Thank you. Different from what I expected, but very useful $\endgroup$ Apr 21, 2013 at 13:22
  • $\begingroup$ Cheers, I hope it gets you closer to a solution, see edit for a correction to the Nest form of iPartition. $\endgroup$ Apr 21, 2013 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.