1 ## Copyright (C) 2008 Michel D. Schmid <michaelschmid@users.sourceforge.net>
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)
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.
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/>.
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.
30 function retmatrix = __rerangecolumns(matrix,analyzeMatrix,nTrainSets)
32 ## check number of inputs
33 error(nargchk(3,3,nargin));
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!
44 nRows = size(analyzeMatrix,1); # get number of rows
50 if ( (analyzeMatrix(i,3)==1) && (analyzeMatrix(i,4)==1) )
57 if ( (analyzeMatrix(i,3)>1) || (analyzeMatrix(i,4)>1) )
64 if (analyzeMatrix(i,1)==1)
76 if (analyzeMatrix(i,2)==1)
77 # easiest case, nothing to do
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);
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);
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) )
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));
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));
115 # now sort binary data, if needed
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));
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));
128 endif# END OF if ( (analyzeMatrix(i,3)>=1) || (analyzeMatrix(i,4)>=1) )
130 endif # END OF if ( (analyzeMatrix(i,3)==1) AND (analyzeMatrix(i,4)==1) )
132 endif # END OF if (analyzeMatrix(i,2)==1)
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];
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];
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);