Message Boards Message Boards

0
|
2889 Views
|
7 Replies
|
0 Total Likes
View groups...
Share
Share this post:
GROUPS:

Sorting data

Posted 10 years ago

Hello,

Here is my variable a:

In[7]:= a

Out[7]= {{2., 3.}, {4., 5.}, {4., 8.}, {4., 9.}, {9., 5.}, {9., 7.}, {10., 1.}}

I would like to sort these data in order to abide by this syntax:

{{x1,y1,...},{x2,y2...}}

What for my variable a would return:

In[7]:= a

Out[7]= {{2., 3.}, {4., 5., 8., 9.},{9., 5., 7.}, {10., 1.}}

How would you proceed to automate this sorting?

Thank you very much

POSTED BY: Michael E
7 Replies
Posted 10 years ago

Thank you.

POSTED BY: Michael E
In[1]:= d = {{2., 3.}, {4., 5., 8., 9.}, {9., 5., 7.}, {10., 1.}};

In[5]:= e = d /. {a_?NumberQ, b__} :> {a, Mean[{b}]}

Out[5]= {{2., 3.}, {4., 7.33333}, {9., 6.}, {10., 1.}}

Note that there is a double underscore after b in the rule since it represents one or more arguments. The NumberQ after a is to prevent it from pattern matching with {2,3}

POSTED BY: Frank Kampas
Posted 10 years ago

Another point please:

How can we get this sorting:

{{2., Mean[{3.}]}, {4., Mean[{5., 8., 9.}]}, {9., Mean[{5., 7.}]}, {10., Mean[{1.}]}}

?

POSTED BY: Michael E
Posted 10 years ago

Thank you very much!

POSTED BY: Michael E
In[1]:= a = {{2., 3.}, {4., 5.}, {4., 8.}, {4., 9.}, {9., 5.}, {9., 
    7.}, {10., 1.}};

In[3]:= b = SplitBy[a, First]

Out[3]= {{{2., 3.}}, {{4., 5.}, {4., 8.}, {4., 9.}}, {{9., 5.}, {9., 
   7.}}, {{10., 1.}}}

In[6]:= c = Transpose /@ b

Out[6]= {{{2.}, {3.}}, {{4., 4., 4.}, {5., 8., 9.}}, {{9., 9.}, {5., 
   7.}}, {{10.}, {1.}}}

In[25]:= d = c /. {n_List, l_List} :>  Join[{n[[1]]}, l]

Out[25]= {{2., 3.}, {4., 5., 8., 9.}, {9., 5., 7.}, {10., 1.}}
POSTED BY: Frank Kampas
Posted 10 years ago

Thank you!

Do you think my desired result can be completely got via Mathematica?

POSTED BY: Michael E

You can get part way to your desired result with SplitBy

In[1]:= a = {{2., 3.}, {4., 5.}, {4., 8.}, {4., 9.}, {9., 5.}, {9., 
    7.}, {10., 1.}};

In[3]:= b = SplitBy[a, First]

Out[3]= {{{2., 3.}}, {{4., 5.}, {4., 8.}, {4., 9.}}, {{9., 5.}, {9., 
   7.}}, {{10., 1.}}}
POSTED BY: Frank Kampas
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract