]> Creatis software - CreaPhase.git/blob - octave_packages/nnet-0.1.13/dividerand.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / nnet-0.1.13 / dividerand.m
1 ## Copyright (C) 2009 Luiz Angelo Daros de Luca <luizluca@gmail.com>
2 ##
3 ##
4 ## This program is free software; you can redistribute it and/or modify it
5 ## under the terms of the GNU General Public License as published by
6 ## the Free Software Foundation; either version 2, or (at your option)
7 ## any later version.
8 ##
9 ## This program is distributed in the hope that it will be useful, but
10 ## WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 ## General Public License for more details.
13 ##
14 ## You should have received a copy of the GNU General Public License
15 ## along with this program; see the file COPYING.  If not, see
16 ## <http://www.gnu.org/licenses/>.
17
18 ## -*- texinfo -*-
19 ## @deftypefn {Function File} [@var{trainVectors},@var{validationVectors},@var{testVectors},@var{indexOfTrain},@var{indexOfValidation},@var{indexOfTest}] = dividerand (@var{allCases},@var{trainRatio},@var{valRatio},@var{testRatio})
20 ## Divide the vectors in training, validation and test group according to
21 ## the informed ratios
22 ##
23 ##
24 ## @example
25 ##
26 ## [trainVectors,validationVectors,testVectors,indexOfTrain,indexOfValidatio
27 ## n,indexOfTest] = dividerand(allCases,trainRatio,valRatio,testRatio)
28 ##
29 ## The ratios are normalized. This way:
30 ##
31 ## dividerand(xx,1,2,3) == dividerand(xx,10,20,30)
32 ##
33 ## @end example
34 ##
35 ## @end deftypefn
36
37
38 function [trainVectors,validationVectors,testVectors,indexOfTrain,indexOfValidation,indexOfTest] = dividerand(allCases,trainRatio,valRatio,testRatio)
39   #
40   # Divide the vectors in training, validation and test group according to
41   # the informed ratios
42   #
43   # [trainVectors,validationVectors,testVectors,indexOfTrain,indexOfValidatio
44   # n,indexOfTest] = dividerand(allCases,trainRatio,valRatio,testRatio)
45   #
46   # The ratios are normalized. This way:
47   #
48   # dividerand(xx,1,2,3) == dividerand(xx,10,20,30)
49   #
50
51   ## Normalize ratios
52   total = trainRatio + valRatio + testRatio;
53   #trainRatio = trainRatio / total; not used
54   validationRatio = valRatio / total;
55   testRatio = testRatio / total;
56
57   ## Calculate the number of cases for each type
58   numerOfCases = size(allCases,2);
59   numberOfValidation = floor(validationRatio*numerOfCases);
60   numberOfTest = floor(testRatio*numerOfCases);
61   numberOfTrain = numerOfCases - numberOfValidation - numberOfTest;
62
63   ## Find their indexes
64   indexOfAll=randperm(numerOfCases);
65   indexOfValidation=sort(indexOfAll(1:numberOfValidation));
66   indexOfTest=sort(indexOfAll((1:numberOfTest)+numberOfValidation));
67   indexOfTrain=sort(indexOfAll((1:numberOfTrain)+numberOfTest+numberOfValidation));
68
69   ## Return vectors
70   trainVectors = allCases(:,indexOfTrain);
71   testVectors = allCases(:,indexOfTest);
72   validationVectors = allCases(:,indexOfValidation);
73 endfunction
74