MATLAB Answers

How to shuffle with replacement every n values of a given dataset into 1000 new sequences

3 views (last 30 days)
Georgios Tertikas
Georgios Tertikas on 30 May 2019
Answered: Jeff Miller on 31 May 2019
I have a sequence of 5000 numbers in a table array of 5000x1. I want to find a way to shuffle with replacement one of every 50 values. for simplicity reasons lets say my sequence is the following: [1 ,2 ,3, 4, 5, 6, ...5000] I would like to randomnly sample with replacement from a subdataset containing the 1st, the 51st, the 101st, the 151st value and so on and then use this value in the 1st position. Then do this for the 2nd value from the subdataset containing the 2nd 52nd 102nd 152nd and so on for every value. It is important that the sampling occurs only from the same dataset and that it is done with replacement. In the end I would like to end up with 1000 new 'random' sequences. I wrote the following code for that:
for k=1:100
Temp= [zeros(50,1000)];
for i=1:50
Pooling= M(i:50:end,2);
Every_50=randi(100,1,1000);
Values=Pooling(Every_50);
Temp(i,:)= Values;
end
M_final(1+(k-1)*50:k*50,2:end)= Temp;
end
Where M(:,2) contains my original sequence of interest.
Using this algorithm I am not sure if it works exactly and using the randi function I am not sure the pooling of the data is done randomnly every time.
If anyone has a better alternative please let me know.

Answers (1)

Jeff Miller
Jeff Miller on 31 May 2019
It might be easier to reshape your matrix so you don't have to worry so much about index bookkeeping. E.g.
Seq = 1:5000;
Increment = 50;
SetsBy50 = reshape(Seq,Increment,[]);
NPerSet = numel(Seq) / Increment;
samples = zeros(NPerSet,Increment);
for i=1:Increment
samplesidx = randi(NPerSet,NPerSet,1);
samples(:,i) = SetsBy50(i,samplesidx(:));
end

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!