]> Creatis software - CreaPhase.git/blob - octave_packages/nan-2.5.5/row_col_deletion.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / nan-2.5.5 / row_col_deletion.m
1 function [rix,cix] = row_col_deletion(d,c,w)
2 % ROW_COL_DELETION selects the rows and columns for removing any missing values. 
3 %    A heuristic based on maximizing the number of remaining sample values
4 %    is used. In other words, if there are more rows than columns, it is 
5 %    more likely that a row-wise deletion will be applied and vice versa. 
6
7 %    [rix,cix] = row_col_deletion(d)
8 %    [rix,cix] = row_col_deletion(d,c,w)
9
10 % Input: 
11 %    d        data (each row is a sample, each column a feature)
12 %    c        classlabels (not really used) [OPTIONAL]
13 %    w        weight for each sample vector [OPTIONAL]   
14 % Output:
15 %    rix      selected samples
16 %    cix      selected columns    
17
18 %   d(rix,cix) does not contain any NaN's i.e. missing values      
19
20 % see also: TRAIN_SC, TEST_SC
21  
22 %       $Id$
23 %       Copyright (C) 2009,2010 by Alois Schloegl <alois.schloegl@gmail.com>
24 %       This function is part of the NaN-toolbox
25 %       http://pub.ist.ac.at/~schloegl/matlab/NaN/
26
27 % This program is free software; you can redistribute it and/or
28 % modify it under the terms of the GNU General Public License
29 % as published by the Free Software Foundation; either version 3
30 % of the  License, or (at your option) any later version.
31
32 % This program is distributed in the hope that it will be useful,
33 % but WITHOUT ANY WARRANTY; without even the implied warranty of
34 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35 % GNU General Public License for more details.
36
37 % You should have received a copy of the GNU General Public License
38 % along with this program; if not, write to the Free Software
39 % Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
40
41
42 if nargin > 2,
43         if isempty(w) || all(w==w(1)), 
44                 ix = ~isnan(c);
45         else
46                 ix = ~any(isnan(c) | isnan(w));
47         end; 
48         d = d(ix,:);    %% ignore samples with invalid c or w
49         w = w(ix,:);
50
51 elseif nargin > 1,
52         d = d(~isnan(c),:);     %% ignore samples with invalid c or w
53         w = [];
54 else
55         w = [];        
56 end;    
57
58
59 if 0, 
60         % decides whether row-wise or column-wise deletion removes less data. 
61         % rix and cix are the resulting index vectors
62         % either row-wise or column-wise deletion, but not a combination of both, is used. 
63         % this is obsolete
64         
65                 n   = numel(d); 
66                 cix = find(~any(isnan(d),1)); 
67                 rix = find(~any(isnan(d),2)); 
68                 nr  = length(rix)*size(d,2); % number of elements after row-wise deletion
69                 nc  = length(cix)*size(d,1); % number of elements after column-wise deletion
70                 
71                 if (nr>nc)
72                         cix = 1:size(d,2);  % select all columns
73                         %fprintf(1,'row-wise deletion (%i,%i,%i)\n',n,nr,nc);           
74                 else
75                         rix = 1:size(d,1);  % select all rows 
76                         %fprintf(1,'column-wise deletion (%i,%i,%i)\n',n,nr,nc);                
77                 end; 
78
79 else
80
81                 %% a mix of row- and column-wise deletion is possible 
82                 if ~isempty(w) && (abs(sum(w)-1) > log2(N)*eps || any(w<0) || any(~isfinite(w)))
83                         error('weight vector must contain only non-negative and finite values');
84                 end; 
85                 [N,M] = size(d); 
86                 rix = ones(N,1); cix = ones(1,M);
87                 while 1;
88                         e = ~isnan(d(rix>0,cix>0));
89                         if  ~isempty(w),
90                                 colCost  = mean(e, 1, w(rix>0)/sum(w(rix>0)))';        % cost of deleting columns
91                         else
92                                 colCost  = mean(e, 1)';        % cost of deleting columns
93                         end;         
94                         rowCost  = mean(e, 2);           % cost of deleting rows     
95                         [tmp,ix] = sort([colCost; rowCost]);
96                         
97                 if abs(tmp(1)-1) < log2(N)*eps, break; end;        % stopping criterion
98
99                         if diff(tmp(1:2))==0, warning('row/col deletion: arbitrary selection [%i,%i]',ix(1:2)); end;  
100                         ix = ix(1);
101                         if (ix<=sum(cix))
102                                 tmp = find(cix>0);
103                                 cix(tmp(ix)) = 0;
104                         else
105                                 tmp = find(rix>0);
106                                 rix(tmp(ix-sum(cix))) = 0;
107                         end;
108                 end; 
109                 rix = find(rix);
110                 cix = find(cix);
111
112 end
113