Tag: crop

Randomly picking equal number of samples for each label in Matlab

No_of_samples are samples which will remain in your data set for each label after you execute the following code:-

classes = unique(labels);
for i=1:numel(classes)
      cur_class_ind = find(labels==classes(i));
      ind_to_remove = cur_class_ind(randperm(numel(cur_class_ind)));
      ind_to_remove = ind_to_remove(1:(numel(cur_class_ind) - no_of_samples));
      labels(ind_to_remove,:) = [];
      data(ind_to_remove,:) = [];

Here ‘data’ is your input dataset with m x n dimension(m=number of samples which we are trying to crop and n =number of features) and ‘labels’ is your vector containing output classes for every corresponding input sample.