TEAP (Toolbox for Emotion Analysis using Physiological Signals) doc
kfold_gen.m
Go to the documentation of this file.
1 function [folds] = kfold_gen(labels,params)
2 
3 if params.nbClasses<2
4 
5  folds = cell(params.nbFolds,1);
6  samples = 1:length(labels);
7  samples = samples(randperm(length(samples)));
8 
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));
13  end
14  folds{params.nbFolds} = samples((k*nbTrialsPerFold)+1:length(labels));
15 
16 else
17  nbTrialsPerClass = zeros(params.nbClasses,1);
18  samples = 1:length(labels);
19  samples = samples(randperm(length(samples)));
20  folds = cell(params.nbFolds,1);
21  if iscell(labels)
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));
26  end
27  folds{params.nbFolds} = samples((k*nbTrialsPerFold):length(labels));
28 
29 
30  else
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)))];
39  end
40  folds{params.nbFolds} = [folds{params.nbFolds} , iCurrentClass((params.nbFolds-1)*nbTrialsPerFold+1:end)];
41  end
42  end
43 end
kfold_gen
function kfold_gen(in labels, in params)