Table/Tree of values

2019-02-20 14:32发布

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 as 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.

1条回答
我命由我不由天
2楼-- · 2019-02-20 15:19

A large part of what you want to do is either very application-specific or more a kind of object oriented view of data structures and code.

But in a first approximation, to help you, here is a little tool which is for many years in my bag of tricks and will complement Map, MapThread and Partition for your kind of problem:

PartitionAs[k_List, c_List] := 
    Map[Take[k, #] &, 
         FoldList[Last[#1] + {1, #2} &, {1, First[c]}, Rest@c]]

PartitionAllAs[k_List, c_List] := 
    Map[Take[k, #] &, 
       If[Last[Last[#]] < Length[k], 
           Append[#, {Last[Last[#]] + 1, Length[k]}], #] &@
                 FoldList[Last[#1] + {1, #2} &, {1, First[c]}, Rest@c]]

Here is an example of what they do

PartitionAs[{a, b, c, d, e, f, g, h, i, j, k}, {1, 2, 5}]

{{a}, {b, c}, {d, e, f, g, h}}


PartitionAllAs[{a, b, c, d, e, f, g, h, i, j, k}, {1, 2, 5}]

{{a}, {b, c}, {d, e, f, g, h}, {i, j, k}}

They have no checks built-in (they do not test if the list of part lengths you send them is compatible with the list size, etc) so it is up to the calling code to be correct, but they may be handy for your application. Also they are only able to specify one depth of partitioning. One could imagine other ways to specify the partitions and it is not very difficult to build more general tree making routines from a flat list. Tell us if you need this kind of things.

查看更多
登录 后发表回答