Question

I am currently using the datasample command in MATLAB to sample data from a 1x9 vector with 9 different numbers. Example code:

test_durations = [5.0000, 5.9460, 7.0711, 8.4090, 10.000, 11.0668, 12.2474, 13.5540,     15.0000]; 
tduration = datasample (test_durations,1,'Replace', false);

This is in a for loop such that on every iteration of the for loop the code pulls a new number from the above vector. So on the first loop it pulls a 5.64960, then on the second loop it pulls a 10, and so on.

My issue is that the code will start resampling data it has already sampled before every data point has been sampled. Is there a way to tell it sample without replacement, and to not resample until all other data points have been sampled first?

I think part of the problem is that the scope of the datasample command within a for loop is limited to that iteration of the loop. I want it to be independent of the loop such that it samples each data point once before resampling.

Was it helpful?

Solution

Here is some code I think will do what you want:

% some random data vector
data = rand(10,1);
N = numel(data);

% number of iterations you are performing
% (could be more or less than the number of data elements)
numIter = 25;

% compute sample indices without replacement
% (resampling will occur if numIter>N)
num = numIter;
idx = cell(ceil(numIter/N),1);
for i=1:numel(idx)
    k = min(num,N);
    num = num - k;
    idx{i} = randperm(N, k);
end
idx = [idx{:}];

% main program loop 
for i=1:numIter
    val = data(idx(i));
    % .. do something with sampled value
end

In the above example I get the following sample indices:

>> idx
idx =
  Columns 1 through 14
     3     8     2     6     1     5    10     7     4     9     4     8     5     7
  Columns 15 through 25
     1    10     6     9     3     2     1     4    10     5     3

>> tabulate(idx)
  Value    Count   Percent
      1        3     12.00%
      2        2      8.00%
      3        3     12.00%
      4        3     12.00%
      5        3     12.00%
      6        2      8.00%
      7        2      8.00%
      8        2      8.00%
      9        2      8.00%
     10        3     12.00%

So for the first ten iterations, it was just a random permutation of the values. The same thing happens for the next ten iterations. Finally in the remaining five loop iterations, 5 random samples out of the ten were chosen.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top