% split feature into training and testing set
function [fea_tr, lab_tr, fea_ts, lab_ts, idx] = split(fea, lab, trper, cn)
% split feature into training and testing set, trper is percentage of training data, cn is classnum

if nargin ~= 4
    disp('split(fea, lab, trper, cn)');
    return;
end
    
fea_tr = [];
lab_tr = [];
fea_ts = [];
lab_ts = [];
idx = [];

% which one to pick up for training
fnum = size(fea,2)/cn;
trnum = trper * fnum;

for k = 1:cn
    i = 0;
    d = zeros(1, fnum);
    while i<trnum
        j = ceil(rand*fnum);
        if d(j) == 0
            d(j) = 1;
            i = i+1;
        end
    end
    f = fea(:, (k-1)*fnum+1:k*fnum);
    l = lab(:, (k-1)*fnum+1:k*fnum);
    fea_tr = [fea_tr, f(:, find(d==1))];
    lab_tr = [lab_tr, l(:, find(d==1))];
    fea_ts = [fea_ts, f(:, find(d==0))];
    lab_ts = [lab_ts, l(:, find(d==0))];
    idx = [idx, d];
end
