3
$\begingroup$

This question already has an answer here:

Let's say I have a set of numbers "r" whose sum equals 15, and I want to randomize all values up to and including that sum from and including 1 into a set called "a". So far I have this:

r = {4,5,6}
ord = Range[15]
a = RandomSample[ord]

And though for everyone the evaluation would yield a randomly different answer each time, for me the answer was:

{4,5,6}
{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}
{13,2,8,15,7,12,14,3,11,1,6,5,9,4,10}

Now, half of the challenge I'm stuck on is how to Partition "a" (the last line of the output above) in the consecutive amounts of the set "r" ({4,5,6}), such that the output next would look like:

{{13,2,8,15},{7,12,4,3,11},{1,6,5,9,4,10}} // Lengths 4,5,6 respectively

Lastly I want the Ordering function applied to each of these subsets in one fell stroke, so that the final output derived from the above line would be:

{{2,3,1,4},{4,3,1,5,2},{1,5,3,2,4,6}}

Of course the answer would look different depending on what random sample was generated, but I need the logic to be consistent with what's here outlined.

Thanks!

$\endgroup$

marked as duplicate by Mr.Wizard Mar 30 '17 at 0:41

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 3
    $\begingroup$ b=Internal`PartitionRagged[a, r] and Ordering/@b? $\endgroup$ – kglr Mar 29 '17 at 23:37
  • $\begingroup$ @kglr That is exactly right, thanks so much! Very elegant : ] $\endgroup$ – Travis Arlen McCracken Mar 29 '17 at 23:41
  • 1
    $\begingroup$ @kglr this is great! what is "Internal`PartitionRagged" I can't find any documentation $\endgroup$ – J42161217 Mar 29 '17 at 23:48
  • 4
    $\begingroup$ @Jenny, it is an undocumented function. Try Information[Internal`PartitionRagged] for a description of what it does. $\endgroup$ – kglr Mar 29 '17 at 23:52
4
$\begingroup$
SeedRandom[1]
r = {4, 5, 6};
ord = Range[15];
a = RandomSample[ord]

{14, 2, 5, 1, 8, 12, 10, 13, 15, 11, 9, 4, 7, 6, 3}

b = Internal`PartitionRagged[a, r]

or, if you have version 10 or a newer version

b = FoldPairList[TakeDrop,a,r]

both give

{{14, 2, 5, 1}, {8, 12, 10, 13, 15}, {11, 9, 4, 7, 6, 3}}

And

Ordering /@ b

{{4, 2, 3, 1}, {1, 3, 2, 4, 5}, {6, 3, 5, 4, 2, 1}}

$\endgroup$

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