1 function [folds] =
kfold_gen(labels,params)
5 folds = cell(params.nbFolds,1);
6 samples = 1:length(labels);
7 samples = samples(randperm(length(samples)));
9 nbTrialsPerFold = floor(length(samples) / params.nbFolds); %imprecision si le nombre de trial par classe n
'est pas divisible par k (params.nbFolds)
10 %On ajoute les indexes pour diviser en k-1 folds
11 for k = 1:params.nbFolds-1
12 folds{k} = samples((k-1)*nbTrialsPerFold+1:(k*nbTrialsPerFold));
14 folds{params.nbFolds} = samples((k*nbTrialsPerFold)+1:length(labels));
17 nbTrialsPerClass = zeros(params.nbClasses,1);
18 samples = 1:length(labels);
19 samples = samples(randperm(length(samples)));
20 folds = cell(params.nbFolds,1);
22 nbTrialsPerFold = round(length(labels) / params.nbFolds); %imprecision si le nombre de trial par classe n'est pas divisible par k (params.nbFolds)
23 %On ajoute les indexes pour diviser en k-1 folds
24 for k = 1:params.nbFolds-1
25 folds{k} = samples((k-1)*nbTrialsPerFold+1:(k*nbTrialsPerFold));
27 folds{params.nbFolds} = samples((k*nbTrialsPerFold):length(labels));
31 for c = 1:params.nbClasses
32 iCurrentClass = find(labels == c);
33 iCurrentClass = randperm(length(iCurrentClass));
34 nbTrialsPerClass(c) = length(iCurrentClass);
35 nbTrialsPerFold = round(nbTrialsPerClass(c) / params.nbFolds); %imprecision si le nombre de trial par classe n
'est pas divisible par k (params.nbFolds)
36 %On ajoute les indexes pour diviser en k-1 folds
37 for k = 1:params.nbFolds-1
38 folds{k} = [folds{k} , iCurrentClass((k-1)*nbTrialsPerFold+1:min(k*nbTrialsPerFold,length(iCurrentClass)))];
40 folds{params.nbFolds} = [folds{params.nbFolds} , iCurrentClass((params.nbFolds-1)*nbTrialsPerFold+1:end)];