vendredi 5 mars 2021

Repeat random, unique sampling of k values n times

In Matlab, I would like to generate a matrix with 4 random, unique samples (out of 10) 7 times. In order to avoid a for-loop, I thought I could just repeat my data and use datasample from Statistics and Machine Learning Toolbox on the first dimension. But it always chooses the same 4 values from each column, so this is kind of useless.

Consider the following MWE:

randomData  = [50.29; 47.72; 48.38; 48.02; 44.23; 47.17; 48.19; 49.11; 50.44; 53.40];

numOfReps               = 7;
numOfSamples            = 4;

randomDataRepMatrix     = randomData*ones(1, numOfReps);

s           = RandStream('mlfg6331_64');
y           = datasample(s, randomDataRepMatrix, numOfSamples, 'Replace', false);

Even without the RandStream part, I get the same results...

Any idea? Or do I need to use the for-loop after all?




Aucun commentaire:

Enregistrer un commentaire