selectively gathering nodes in a tree

I have a tree (expression) on which I want to gather only certain types of nodes - those that follow a certain pattern. I have a simplified example below:

A = {{{{},{0.3,0.3}},{0.2,0.2}},{0.1,0.1}};
TreeForm[A, PlotRangePadding->0]
Cases[A, {x_Real, y_Real}, Infinity]

The output:

在这里输入图像描述

Is this a good way to do it?

If instead of {x_, y_}, if I wanted to look for {{x1_, y1_}, {x2_, y_2}}, how can I exclude expressions like {x_, y_}, which also match?

Regards

EDIT (14/07/2011)

I have found that using a head other than List will greatly aid in finding such sub-expressions without collisions.

For example, reformulating the above:

A = {{{{}, pt[0.3, 0.3]}, pt[0.2, 0.2]}, pt[0.1, 0.1]};
List @@@ Cases[A, _pt, Infinity]

Output:

{{0.3,0.3},{0.2,0.2},{0.1,0.1}}

About the second part of your question, ie, selecting {{a,b},{c,d}} , how about

b = {{{{}, {0.3, 0.3}}, {0.2, 0.2}}, {{0.1, 0.1}, {0.3, 0.4}}};
TreeForm[b]
Cases[b, {{a_, b_}, {c_, d_}} /; (And @@ NumericQ /@ {a, b, c, d}), Infinity]

(so that they do not have to be Real but any Numeric will do)?


这里是acl使用的形式的替代方案,我发现更具可读性。

b = {{{{}, {0.3, 0.3}}, {0.2, 0.2}}, {{0.1, 0.1}, {0.3, 0.4}}};

With[{p = _?NumericQ}, Cases[b, {{p, p}, {p, p}}, -1] ]
链接地址: http://www.djcxy.com/p/35580.html

上一篇: Mathematica中如何链接这个线性求解器?

下一篇: 选择性地收集树中的节点