]> Creatis software - CreaPhase.git/blob - octave_packages/nnet-0.1.13/__rerangecolumns.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / nnet-0.1.13 / __rerangecolumns.m
1 ## Copyright (C) 2008 Michel D. Schmid  <michaelschmid@users.sourceforge.net>
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{retmatrix} = __rerangecolumns (@var{matrix},@var{analyzeMatrix},@var{nTrainSets})
20 ## @code{__rerangecolumns} reranges the data sets depending on the input arguments.
21 ## @code{matrix} is the data set matrix containing inputs and outputs (targets) in row order.
22 ## This means for example: the first three rows are inputs and the fourth row is an output row.
23 ## The second argument is used in the optimizing algorithm. This matrix contains informations about
24 ## the description of the rows data of matrix.
25 ## The third argument is used to be sure, rerange all the columns to the correct position.
26 ## @end deftypefn
27
28 ## Author: mds
29
30 function retmatrix = __rerangecolumns(matrix,analyzeMatrix,nTrainSets)
31
32   ## check number of inputs
33   error(nargchk(3,3,nargin));
34
35   # set default values
36
37   # now sort "matrix" with help of analyzeMatrix
38   # following conditions must be kept:
39   # a.) rows containing unique values aren't sorted!
40   # b.) sort first rows which contains min AND max values only once
41   # c.) sort secondly rows which contains min OR max values only once
42   # d.) at last, sort binary data if still needed!
43
44   nRows = size(analyzeMatrix,1);   # get number of rows
45
46   ## create i-vector
47   i = 1;
48   iVec = [];
49   while (i <= nRows)
50     if ( (analyzeMatrix(i,3)==1) && (analyzeMatrix(i,4)==1) )
51       iVec = [iVec i];
52     endif
53     i += 1;
54   endwhile
55   i = 1;
56   while (i <= nRows)
57         if ( (analyzeMatrix(i,3)>1) || (analyzeMatrix(i,4)>1) )
58           iVec = [iVec i];
59         endif
60         i += 1;
61   endwhile
62   i = 1;
63   while (i <= nRows)
64     if (analyzeMatrix(i,1)==1)
65       iVec = [iVec i];
66     endif
67   i += 1;
68   endwhile
69
70
71   ## now do main loop
72   j = 1;
73   i = iVec(j);
74   nRows = length(iVec);
75   while (j < nRows)
76     if (analyzeMatrix(i,2)==1)
77       # easiest case, nothing to do
78     else
79
80       # now let's see if min AND max values are only once in the row
81       if ( (analyzeMatrix(i,3)==1) && (analyzeMatrix(i,4)==1) )
82                 # search at which index the min value is
83                 minVal = min(matrix(i,:));
84         [rowInd, colInd] = find(matrix(i,:)==minVal);# colInd is searched
85         if (colInd >= nTrainSets ) # move column
86                   matrix = __copycoltopos1(matrix,colInd);
87         endif
88         # search at which index the max value is
89         maxVal = max(matrix(i,:));
90         [rowInd, colInd] = find(matrix(i,:)==maxVal);# colInd is searched
91         if (colInd >= nTrainSets ) # move column
92                   matrix = __copycoltopos1(matrix,colInd);
93         endif
94
95       else
96         
97                 # now here, we have to copy the rows, if min OR max values are more than once in a row
98         if ( (analyzeMatrix(i,3)>=1) || (analyzeMatrix(i,4)>=1) )
99
100                   # search at which index the min value is
101                   minVal = min(matrix(i,:));
102           [rowInd, colInd] = find(matrix(i,:)==minVal);# colInd is searched
103           if (colInd(1) >= nTrainSets ) # move column
104                     matrix = __copycoltopos1(matrix,colInd(1));
105           endif
106           
107           # search at which index the max value is
108           maxVal = max(matrix(i,:));
109           [rowInd, colInd] = find(matrix(i,:) == maxVal);# colInd is searched
110           if (colInd(1) >= nTrainSets ) # move column
111                     matrix = __copycoltopos1(matrix,colInd(1));
112           endif
113
114                 else
115                   # now sort binary data, if needed
116                   
117           # search at which index the 0-value is
118                   [rowInd, colInd] = find(matrix(i,:)==0);# colInd is searched
119           if (colInd(1) >= nTrainSets ) # move column
120                     matrix = __copycoltopos1(matrix,colInd(1));
121           endif
122           # search at which index the 1-value is
123           [rowInd, colInd] = find(matrix(i,:)==1);# colInd is searched
124           if (colInd(1) >= nTrainSets ) # move column
125                     matrix = __copycoltopos1(matrix,colInd(1));
126           endif
127
128         endif# END OF if ( (analyzeMatrix(i,3)>=1) || (analyzeMatrix(i,4)>=1) )
129
130       endif # END OF if ( (analyzeMatrix(i,3)==1) AND (analyzeMatrix(i,4)==1) )
131
132     endif # END OF if (analyzeMatrix(i,2)==1)
133     j += 1;
134     i = iVec(j);
135   endwhile
136   retmatrix = matrix;
137 endfunction
138
139 %!shared matrix,analyzeMatrix,nTrainSets, returnmatrix
140 %! disp("testing __rerangecolumns")
141 %! matrix = [0 1 0 0 0 0 1 0 1 1;  \
142 %!                       4 4 4 4 4 4 4 4 4 4;  \
143 %!        -1.1 -1.1 2 3 4 3.2 1 8 9 10; \
144 %!           0 1.1 3 4 5 2 10 10 2 3; \
145 %!          -1 1 1 1 1 2 3 4 1 5];
146 %! analyzeMatrix = [1 0 0 0; 0 1 0 0; 0 0 2 1; 0 0 1 2; 0 0 1 1];
147 %! nTrainSets = 8;
148 %! returnmatrix = __rerangecolumns(matrix,analyzeMatrix,nTrainSets);
149 %!assert(returnmatrix(1,1)==1);
150 %!assert(returnmatrix(2,1)==4);
151 %!assert(returnmatrix(3,1)==1);
152 %!assert(returnmatrix(4,1)==10);
153 %!assert(returnmatrix(5,1)==3);
154 %! matrix = [0 1 0 0 0 0 1 0 1 1;                       \
155 %!                       4 4 4 4 4 4 4 4 4 4;                   \
156 %!          -1.1 -1.1 2 3 4 3.2 1 8 9 10;       \
157 %!           0 1.1 3 4 5 2 10 10 2 3;           \
158 %!          -1 1 1 1 1 2 3 4 1 5;               \
159 %!                       0 1 2 1 2 1 2 3 4 5;];  # the last row is euqal to the nnet targets
160 %! analyzeMatrix = [1 0 0 0; 0 1 0 0; 0 0 2 1; 0 0 1 2; 0 0 1 1];
161 %! nTrainSets = 8;
162 %! returnmatrix = __rerangecolumns(matrix,analyzeMatrix,nTrainSets);
163 %!assert(returnmatrix(1,1)==1);
164 %!assert(returnmatrix(2,1)==4);
165 %!assert(returnmatrix(3,1)==1);
166 %!assert(returnmatrix(4,1)==10);
167 %!assert(returnmatrix(5,1)==3);
168 %!assert(returnmatrix(6,1)==2);