0
|
2586 Views
|
7 Replies
|
0 Total Likes
View groups...
Share
GROUPS:

# Sorting data

Posted 9 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
7 Replies
Sort By:
Posted 9 years ago
 Thank you.
Posted 9 years ago
 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 9 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 9 years ago
 Thank you very much!
Posted 9 years ago
 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 9 years ago
 Thank you!Do you think my desired result can be completely got via Mathematica?
Posted 9 years ago
 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.}}} 
Community posts can be styled and formatted using the Markdown syntax.