MATLAB: Random picking of adjacent data points

data setsrandom

Hi,
May someone help me
I have data in one column and want to randomly pick adjacent 48 data points for 1000 times. For example, we have 10 data point (1, 2,3, 4,5,6,7,8,9,10) and we randomly pick 3 adjacent data points 5 times.
May some one help me …
Thank you.

Best Answer

Try this:
data = [1:50000]'; % Whatever.....
% Define general parameters for getting the samples:
numElements = size(data, 1)
numAdjacent = 48
numSubsets = 1000
% Now do the iterations to get each of the 1000 subsets.
for k = 1 : numSubsets
% Get the first index at random. Don't get within numAdjacent of the end though!
firstIndex = randi(numElements - numAdjacent + 1);
% Now get the second index which will make the subset have adjacent indexes.
secondIndex = firstIndex + numAdjacent - 1;
% Get one subset of 48 adjacent data samples.
theseData = data(firstIndex : secondIndex);
% Now do something with this set of adjacent elements.
end