Take the 2-minute tour ×

What is the best way to do random sample with replacement from dataset? I am using 316 * 34 as my dataset. I want to segment the data into three buckets but with replacement. Should I use the randperm because I need to make sure I keep the index intact where that index would be handy in identifying the label data. I am new to matlab I saw there are couple of random sample methods but they didn't look like its doing what I am looking for, its strange to think that something like doesn't exist in matlab, but I did the follwoing:

My issue is when I do this row_idx = round(rand(1)*316) sometimes I get zero, that leads to two questions

  1. what should I do to avoid zeor?
  2. What is the best way to do the random sample with replacement.

        shuffle_X = X(randperm(size(X,1)),:);
        lengthOf_shuffle_X = length(shuffle_X)
        number_of_rows_per_bucket = round(lengthOf_shuffle_X / 3)
        bucket_cell = cell(3,1)
        bag_matrix = []
        for k = 1:length(bucket_cell)
            for i = 1:number_of_rows_per_bucket
                row_idx = round(rand(1)*316)
                bag_matrix(i,:) = shuffle_X(row_idx,:)
            bucket_cell{k} = bag_matrix

I could do following:

            if row_idx == 0
                row_idx = round(rand(1)*316)

assuming random number will never give two zeros values in two consecutive rounds.

share|improve this question

2 Answers 2

randi is a good way to get integer indices for sampling with replacement. Assuming you want to fill three buckets with an equal number of samples, then you can write

data = rand(316,34); %# create some dummy data
number_of_data = size(data,1);
number_of_rows_per_bucket = 50;
bucket_cell = cell(1,3);

idx = randi([1,number_of_data],[number_of_rows_per_bucket,3]);

for iBucket = 1:3
   bucket_cell{iBucket} = data(idx(:,iBucket),:);
share|improve this answer
Not sure if you meant to type "with replacement" instead of "without replacement" I am looking for "with replacement" –  Null-Hypothesis Nov 17 '12 at 22:19
@Null-Hypothesis: Oops, I mis-typed. It's of course sampling with replacement. –  Jonas Nov 18 '12 at 1:14

To the question: if you use randperm it will give you a draw order without replacement, since you can draw any item once.

If you use randi it draws you with replacement, that is you draw an item possibly many times.

If you want to "segment" a dataset, that usually means you split the dataset into three distinct sets. For that you use draw without replacement (you don't put the items back; use randperm). If you'd do it with replacement (using randi), it will be incredibly slow, since after some time the chance that you draw an item which you have not before is very low. (Details in coupon collector ).

If you need a segmentation that is a split, you can just go over the elements and independently decide where to put it. (That is you choose a bucket for each item with replacement -- that is you put any chosen bucket back into the game.)

For that:

% if your data items are vectors say data = [1 1; 2 2; 3 3; 4 4]
num_data = length(data);
bucket_labels = randi(3,[1,num_data]); % draw a bucket label for each item, independently.
for i=1:3
  bucket{i} = data(bucket_labels==i,:); 

%if your data items are scalars say data = [1 2 3 4 5]    
num_data = length(data);
for i=1:3
  bucket{i} = data(bucket_labels==i); 

there we go.

share|improve this answer
is it possible you could explain the logic behind following lines in the first solution bucket_labels = randi(3,[1,num_data]); Thanks, –  Null-Hypothesis Nov 18 '12 at 5:14
It takes a matrix with shape [1,num_data], (that is a num_data long vector), and fills it up with independently drawn random numbers, with uniform distribution on the set {1, 2, 3}. –  Barnabas Szabolcs Nov 19 '12 at 18:54

Your Answer


