function test_inds = train_test_split(class_inds,percent_train_per_class)

%class_inds should be a vector of finitely many class labels; say K of them
%train_per_class is a length-K vector of values in (0,1), that informs
% approximately how much of each class should be used for training data.
% If a scalar is input, we use that value for each class.
%test_inds is a boolean vector, true when the given index is in the
%training set.

uclass = unique(class_inds);
K = length(uclass);
nclass = histc(class_inds,uclass);
N = length(class_inds);
randbool = @(n,p) rand(n,1)<p;

if isscalar(percent_train_per_class)
percent_train_per_class = percent_train_per_class*ones(K,1);
end

test_inds = false(N,1);

for ii=1:K
the_inds = randbool(nclass(ii),percent_train_per_class(ii));
test_inds(class_inds==uclass(ii)) = the_inds;
end

return
end