function [indexes]=train_test_random_new(y,n,nall)
%
%        [indexes]=train_test_random_new(y,n,nall)
%
%  This function  ramdonly selects training samples and testing samples 
%  from the  ground truth image.
% 
%------------------- input parameters -------------------------------------
%
%  y     --   ground truth 
%  n     --   number of labeled  samples per class
%  nall  --   number of labeled samples in total
%
%  ---------------output parameters ---------------------------------------
%
% indexes  -- index of  labeled samples 
%
%
%% NOTE: The final number of samples is given by:  max{nall, K*n} 
%%                  where K is the number of classes.
%
% Author  Jun Li, Sep, 2008

K = max(y);

% generate the  training set
indexes = [];
for i = 1:K
    index1 = find(y == i);
    per_index1 = randperm(length(index1));
    if length(index1)>n
        indexes = [indexes; index1(per_index1(1:n))'];
    else
        indexes = [indexes; index1'];
    end
end

indexes_all = [1:length(y)];
indexes_all(indexes) = [];
n_new = nall - length(indexes);
per_indexall = randperm(length(indexes_all));
indexes_new = indexes_all(per_indexall(1:n_new));
indexes = [indexes;indexes_new'];


                  