1
$\begingroup$

I have an Association which contains three arrays 200x1000x2.
Each row m of a matrix denotes a different set of parameters, while each column n contains the outcomes of the function for a specific value of x. Each elements is {x,f_m(x)}.

I want to sort each {x,f_m(x)} in a column in ascending order by f(x), so that the first row will now contains the lowest values of f(x), while the last row will contain the highest. Moreover, the elements should stay in their original column, so that all the elements in a column keep having the same value of x.
The rows will then be used to create a function that represents the envelope of the different outcomes of a Monte Carlo method.

I tried using Sort, together with Transpose, but the result is messed up.

Data Example with three 4x3x2 arrays:

<| 1-> 
{
 {{0.1,2.1},{0.2,0.7}, {0.5,-3.5}},
 {{0.1,4.2},{0.2,5.5}, {0.5,3.1} },
 {{0.1,5.6},{0.2,-6.4},{0.5,0.2} },
 {{0.1,7.4},{0.2,7.2}, {0.5,-8.2}}
},
2-> 
{
 {{0.1,4.5}, {0.2,3.5}, {0.5,5.6} },
 {{0.1,-3.1},{0.2,1.4}, {0.5,-7.4}},
 {{0.1,1.3}, {0.2,3.4}, {0.5,4.27}},
 {{0.1,-3.6},{0.2,-2.5},{0.5,0.2} }
},
3-> 
{
 {{0.1,3.6}, {0.2,7.2},{0.5,9.1} },
 {{0.1,-1.4},{0.2,1.3},{0.5,5.4} },
 {{0.1,2.3}, {0.2,3.4},{0.5,2.4} },
 {{0.1,-3.7},{0.2,5.6},{0.5,-6.2}}
}|>

and I want to obtain something like this

<| 1-> 
{
 {{0.1,2.1},{0.2,-6.4},{0.5,-8.2}},
 {{0.1,4.2},{0.2,0.7}, {0.5,-3.5}},
 {{0.1,5.6},{0.2,5.5}, {0.5,0.2} },
 {{0.1,7.4},{0.2,7.2}, {0.5,3.1} }
},
2-> 
{
 {{0.1,_3.6}, {0.2,-2.5},{0.5,-7.4}},
 {{0.1,-3.1},{0.2,1.4}, {0.5,0.2} },
 {{0.1,1.3}, {0.2,3.4}, {0.5,4.27}},
 {{0.1,4.5},{0.2,3.5}, {0.5,5.6} }
},
3-> 
{
 {{0.1,-3.7}, {0.2,1.3},{0.5,-6.2}},
 {{0.1,-1.4},{0.2,3.4}, {0.5,2.4} },
 {{0.1,2.3}, {0.2,5.6}, {0.5,5.4} },
 {{0.1,3.6}, {0.2,7.2}, {0.5,9.1} }
}|>
$\endgroup$
  • 1
    $\begingroup$ Example data please. $\endgroup$ – Henrik Schumacher Apr 25 at 20:56
  • $\begingroup$ Instead of three 4x3 matrices, these are 3 arrays of dimensions {4,3,2}. $\endgroup$ – Henrik Schumacher Apr 25 at 21:40
  • $\begingroup$ Do you mean Map[#[[Ordering[#[[All, 2]]]]] &, asso, {2}]? $\endgroup$ – Coolwater Apr 25 at 21:45
  • $\begingroup$ Thanks for the answer Coolwater, however that function does not preserve the structure of the association. I'd like to keep the elements in their original column. Edited the text for more clarity. $\endgroup$ – 254 Apr 25 at 21:59
  • 1
    $\begingroup$ I think you might have an error in your "after" example. If so, I believe AssociationThread[ Keys[#] -> Transpose /@ (Map[SortBy[Last], #] & /@ Transpose /@ Values@#)] & applied to your association does what you're after. $\endgroup$ – ciao Apr 25 at 23:20
0
$\begingroup$
a = <| 1-> 
{
 {{0.1,2.1},{0.2,0.7}, {0.5,-3.5}},
 {{0.1,4.2},{0.2,5.5}, {0.5,3.1} },
 {{0.1,5.6},{0.2,-6.4},{0.5,0.2} },
 {{0.1,7.4},{0.2,7.2}, {0.5,-8.2}}
},
2-> 
{
 {{0.1,4.5}, {0.2,3.5}, {0.5,5.6} },
 {{0.1,-3.1},{0.2,1.4}, {0.5,-7.4}},
 {{0.1,1.3}, {0.2,3.4}, {0.5,4.27}},
 {{0.1,-3.6},{0.2,-2.5},{0.5,0.2} }
},
3-> 
{
 {{0.1,3.6}, {0.2,7.2},{0.5,9.1} },
 {{0.1,-1.4},{0.2,1.3},{0.5,5.4} },
 {{0.1,2.3}, {0.2,3.4},{0.5,2.4} },
 {{0.1,-3.7},{0.2,5.6},{0.5,-6.2}}
}|>

Map[Transpose[SortBy[Last] /@ Transpose[#]] &, a]

<|

1 -> {

{{0.1, 2.1}, {0.2, -6.4}, {0.5, -8.2}},

{{0.1, 4.2}, {0.2, 0.7}, {0.5, -3.5}},

{{0.1, 5.6}, {0.2, 5.5}, {0.5, 0.2}},

{{0.1, 7.4}, {0.2, 7.2}, {0.5, 3.1}}

},

2 -> {

{{0.1, -3.6}, {0.2, -2.5}, {0.5, -7.4}},

{{0.1, -3.1}, {0.2, 1.4}, {0.5, 0.2}},

{{0.1, 1.3}, {0.2, 3.4}, {0.5, 4.27}},

{{0.1, 4.5}, {0.2, 3.5}, {0.5, 5.6}}

},

3 -> {

{{0.1, -3.7}, {0.2, 1.3}, {0.5, -6.2}},

{{0.1, -1.4}, {0.2, 3.4}, {0.5, 2.4}},

{{0.1, 2.3}, {0.2, 5.6}, {0.5, 5.4}},

{{0.1, 3.6}, {0.2, 7.2}, {0.5, 9.1}}

} |>

$\endgroup$
  • $\begingroup$ Thanks, this solution works! I had some problems explaining what I wanted to achieve, but you got it. Both yours and ciao's solution works well. $\endgroup$ – 254 Apr 26 at 0:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.