值表/树 [英] Table/Tree of values

查看:20
本文介绍了值表/树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题:

有没有办法创建价值树?类似于命令 TreeForm 的输出,但是在节点中有值?

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

我为什么要这个?

我正在尝试编写一个完整的程序来分析我的实验室课程的输出.每列数据作为一个符号分配.一般来说,每一列都是有意义的:它不仅仅是一堆不同的变量.我想说的是,一般情况下,计算是按列"完成的.我的问题是,当我需要进行需要更复杂的水平"结构的计算时:将变量分配给列缺乏水平灵活性".(在某种程度上,这是在 Excel 中用 $$ 和数组公式解决的那种问题)

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)

举个例子:

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

我想将 y 的 1;;3, 4;;8, 9;;9 部分与 x 的每个元素相关联.我所说的关联是指对于某些计算,函数的输入将作为每个集合的参数.

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.

我知道 Map、Apply、Thread 和 MapThread 等函数.我一直在用它们来解决这类问题,但有时会让人有点困惑.

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.

我也知道分区,如果我想在相同长度的子数组中将 y 分开,这将解决我的问题.

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

示例:计算list的lenghts n={3,2,5}的不规则分区的均值和均方偏差

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

我非常概念化的方法:

我的表/树的第一列将是数据 y.为了引用某个列上的某个值,我将使用一对坐标 i,j:i 代表列,j 代表内部位置.我会将坐标 i=1 分配给 y.

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]

均值列 Xav i=2 将有 3 个元素.我为每个人分配了一个连接列表给 y:

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

("" 与 C"" 的连接)

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

连接写成形式{i,{j 的 i}} 的元素

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]

此列为 i=3 以及 3 个元素.

This column as i=3 and also 3 elements.

对于这个计算,我想使用 i=1,2 列.y 的计算连接与用于计算 Yav 的连接相同.但是现在,我需要将 Ymsd 连接到 Yav.

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.

分配连接后,我将使用一个函数来获取正确的值,由创建的地图引导,并应用 F1、F2.

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.

推荐答案

您想做的大部分事情要么是非常特定于应用程序的,要么更多的是一种面向对象的数据结构和代码视图.

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.

但是,为了帮助您,这里有一个小工具,它在我的技巧包中使用了很多年,可以为您解决此类问题补充 Map、MapThread 和 Partition:

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.

这篇关于值表/树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆