Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to randomly sample data with seeding?

I would like to randomly choose elements from a finite set that contains both numbers and NaNs while seeding the random number generation procedure.

So far I can make it work without seeding:

data = [0, 1, 2, 3, 4, 5, nan];
sample = datasample(data, 50);

but if I want to seed the number generation:

seed = rng(100);
sample = datasample(seed, data, 50);

I get the following error:

Error using datasample (line 89)
Sample size K must be a non-negative integer.

even if the syntax for datasample is (*):

[y,...] = datasample(s,data,k,...)

I have tried using randsample, too, but I get similar results.

(*) https://it.mathworks.com/help/stats/datasample.html

like image 852
Pier Paolo Avatar asked Mar 21 '26 04:03

Pier Paolo


1 Answers

The documentation isn't super explicit about the first input. You need to pass a RandStream object as the first input argument rather than the struct that rng generates (As a sidenote, the output of rng is the previous setting not the new settings).

Here is the equivalent of what it seems you were trying to do

stream = RandStream('mt19937ar', 'Seed', 100);
output = datasample(stream, data, k);

If you want to instead use rng to specify the seed, you can call rng and then use RandStream.getGlobalStream to get the current global random number stream and then pass that to datasample. This is slightly redudant though since datasample is going to use the global random number stream if one isn't provided.

rng(100)
stream = RandStream.getGlobalStream();
output = datasample(stream, data, k);
like image 122
Suever Avatar answered Mar 24 '26 00:03

Suever



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!