Re: Grouping {x,y z} data set
- To: mathgroup at smc.vnet.net
- Subject: [mg99615] Re: Grouping {x,y z} data set
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Sat, 9 May 2009 03:21:53 -0400 (EDT)
On 5/8/09 at 12:18 AM, graser at gmail.com (graser) wrote:
>I have arbitrary data set.
>For example,
>Data={{x_1,y_1,z_1},{x_2,y_2,z_1},{x_1, y_2, z_3},........{x_n, y_m,
>z_L}}
I assume this is *not* what you actually entered into
Mathematica. If it was, entering data literally as x_1 will
definitely be a problem
>I tried to group this data set depending on z value.
>I mean I try to select all x, y values with same z value.
In version 7, this is easily done using GatherBy. For example,
In[3]:= data =
ArrayFlatten@{{RandomReal[1, {10, 2}],
Transpose@{RandomInteger[1, {10}]}}};
In[4]:= GatherBy[data, Last]
Out[4]= {{{0.654913, 0.942989, 1}, {0.899263, 0.0620125,
1}, {0.872745, 0.876903, 1}, {0.99796, 0.425091, 1}, {0.399351,
0.169411, 1}, {0.435277, 0.683664, 1}}, {{0.857486, 0.913224,
0}, {0.951074, 0.237923, 0}, {0.528556, 0.0531316, 0}, {0.796729,
0.942358, 0}}}
In earlier versions of Mathematica this can be achieve using
Split after ordering the data appropriately
Split[data[[Ordering[Last/@data]]],Last#1==Last#2&]
will group the data according to the last value in each row.
Note, while this will group the data in a manner equivalent to
GatherBy, the end result will typically have a different order.