function mkperms
%MKPERMS  Generate permutation files for the datasets.

% an arbitrary seed for replication purpose
rand('state', 98132474136781);

save_perm('dna',       2000,  1186);
save_perm('glass',      214,     0);
save_perm('iris',       150,     0);
save_perm('letter',   16000,  4000);
save_perm('pendigits', 7494,  3498);
save_perm('satimage',  4435,  2000);
save_perm('segment',   2310,     0);
save_perm('shuttle',  43500, 14500);
save_perm('vehicle',    846,     0);
save_perm('vowel',      528,   462);
save_perm('wine',       178,     0);

% randomize it for other commands
rand('state', sum(100*clock));

function save_perm (dat, train, test)
if test == 0
    idx = equal_folds(train, 10, 10);
else
    idx = all_same(train+test, 100);
end
savedata([dat '.perm'], idx - 1); % starting from 0

function idx = all_same (n, r)
idx = repmat(1:n, r, 1);

function idx = equal_folds (n, k, r)
idx = zeros(k*r, n);
for i = 1:k:k*r
    idx(i:i+k-1,:) = equal_fold(n, k);
end

function [idx,tsize] = equal_fold (n, k)
%EQUAL_FOLD  Similar to K-fold but use equal-size test sets.
idx = zeros(k, n);
tsize = round(n / k);
rp = randperm(n);
for i = 1:k
    e = floor(i*n/k);
    idx(i,:) = rp([e+1:end 1:e]);
end
