Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a list called cant with some amounts of samples to take from the list community as follows:

cant = {5, 1, 2, 3, 4};
community = {{1, 2, 3, 4, 5, 6}, {1, 2, 3}, {1, 2, 3, 4}, {1, 2, 3}, {1, 2, 3, 4, 5, 6}}; 

I'm trying to find a better way to do this task:

where = {};
Table[AppendTo[where, 
 Table[Flatten[{RandomSample[community[[i]], 1], i}], {cant[[
    i]]}]];, {i, Length[community]}];

The result is:

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

Because with

where[[1]]
{{3, 1}, {2, 1}, {3, 1}, {4, 1}, {4, 1}}

The 5 samples in the first item (3, 2, 3, 4, and 4) have the index of community 1. I read about Reap and Sow, but really I couldn't understand how to implement this. Any idea of how to make this faster because I'm working with large communities and sampling thousands?

I appreciate your help,

share|improve this question

3 Answers 3

up vote 5 down vote accepted
MapThread[Thread[List[RandomChoice[#1, #2], #3]] &, {community, cant, Range@Length@cant}]

(* {{{2, 1}, {1, 1}, {3, 1}, {4, 1}, {6, 1}}, {{3, 2}}, {{4, 3}, {1, 
   3}}, {{2, 4}, {3, 4}, {1, 4}}, {{2, 5}, {3, 5}, {1, 5}, {6, 5}}} *)

Some variations:

MapIndexed[Thread[List[#1, First@#2]] &, MapThread[RandomChoice[#1, #2] &, {community, cant}]]
MapIndexed[Function[i, Join[{i}, #2]] /@ #1 &, MapThread[RandomChoice[#1, #2] &, {community, cant}]]
share|improve this answer
    
I believe RandomSample should be RandomChoice. –  Pickett Aug 7 at 22:07
    
I agree, though it depends on OP's goal (if there should be any repeated values in the sample). Also @Mr.Wizard I finally know what you were talking about here, about nesting pure functions. –  seismatica Aug 7 at 22:11
2  
I think the OP's code is unambiguous in this regard. It should be RandomChoice (though he correctly uses RandomSample in his code, in a different way). –  Pickett Aug 7 at 22:13
    
Ah now I see your point. Evaluating Table[RandomSample[community[[1]],1],{4}] several times helped me see what's going on. I fixed my answer based on your recommendation. Thanks so much! –  seismatica Aug 7 at 22:17
    
I created a function with the first line you gave me and works better than my initial idea, thanks! –  Jotasmall Aug 8 at 14:33
{(community[[#]]~RandomSample~1)[[1]], #} & /@ 
   ConstantArray[#, cant[[#]]] & /@ Range[Length@cant]
share|improve this answer
Thread[{RandomSample[community[[#]], cant[[#]]], #}] & /@ Range[Length[community]]
(* {{{2, 1}, {3, 1}, {5, 1}, {1, 1}, {4, 1}}, 
    {{3, 2}},
    {{2, 3}, {4, 3}},
    {{3, 4}, {2, 4}, {1, 4}},
    {{3, 5}, {5, 5}, {4, 5}, {1, 5}}} *)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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