]> Creatis software - CreaPhase.git/blob - octave_packages/nan-2.5.5/cat2bin.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / nan-2.5.5 / cat2bin.m
1 function [B,BLab]=cat2bin(D, Label, MODE)
2 % CAT2BIN converts categorial into binary data 
3 %   each category of each column in D is converted into a logical column
4
5 %   B = cat2bin(C); 
6 %   [B,BinLabel] = cat2bin(C,Label); 
7 %   [B,BinLabel] = cat2bin(C,Label,MODE)
8 %
9 %  C        categorial data 
10 %  B        binary data 
11 %  Label    description of each column in C
12 %  BinLabel description of each column in B
13 %  MODE     default [], ignores NaN
14 %           'notIgnoreNAN' includes binary column for NaN 
15 %           'IgnoreZeros'  zeros do not get a separate category 
16 %           'IgnoreZeros+NaN' zeros and NaN are ignored
17 %
18 %  example: 
19 %     cat2bin([1;2;5;1;5]) results in 
20 %             1     0     0
21 %             0     1     0
22 %             0     0     1
23 %             1     0     0
24 %             0     0     1
25
26 %       $Id: cat2bin.m 9033 2011-11-08 20:58:07Z schloegl $
27 %       Copyright (C) 2009 by Alois Schloegl <alois.schloegl@gmail.com>
28 %       This function is part of the NaN-toolbox
29 %       http://pub.ist.ac.at/~schloegl/matlab/NaN/
30
31 % This program is free software; you can redistribute it and/or
32 % modify it under the terms of the GNU General Public License
33 % as published by the Free Software Foundation; either version 3
34 % of the  License, or (at your option) any later version.
35
36 % This program is distributed in the hope that it will be useful,
37 % but WITHOUT ANY WARRANTY; without even the implied warranty of
38 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
39 % GNU General Public License for more details.
40
41 % You should have received a copy of the GNU General Public License
42 % along with this program; if not, write to the Free Software
43 % Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
44
45 if nargin<3,
46          MODE = []; 
47 end; 
48
49 % convert data 
50 B = []; 
51
52 c     = 0; 
53 k1    = 0; 
54 BLab  = [];
55 for m = 1:size(D,2) 
56         h = histo_mex(D(:,m));
57         x = h.X(h.H>0);
58         if strcmpi(MODE,'notIgnoreNaN')
59                 ;
60         elseif strcmpi(MODE,'IgnoreZeros')
61                 x = x(x~=0);
62         elseif strcmpi(MODE,'IgnoreZeros+NaN')
63                 x = x((x~=0) & (x==x));
64         else 
65                 x = x(x==x);
66         end; 
67         for k = 1:size(D,1),
68                 if ~isnan(D(k,m))
69                         B(k, c + find(D(k,m)==x)) = 1;
70                 elseif isnan(x(end)),
71                         B(k, c + length(x)) = 1;
72                 end;
73         end;
74
75         c = c + length(x);
76         if nargout>1,
77                 for k = 1:length(x),
78                         k1 = k1+1;
79                         if isempty(Label)
80                                 BLab{k1} = ['#',int2str(m),':',int2str(x(k))];
81                         else
82                                 BLab{k1} = [Label{m},':',int2str(x(k))];
83                         end;
84                 end;
85         end;
86 end;
87
88
89 %!assert(cat2bin([1;2;5;1;5]),[1,0,0;0,1,0;0,0,1;1,0,0;0,0,1])
90