]> Creatis software - CreaPhase.git/blob - octave_packages/dataframe-0.9.1/@dataframe/private/df_mapper2.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / dataframe-0.9.1 / @dataframe / private / df_mapper2.m
1 function resu = df_mapper2(func, df, varargin)
2   %# resu = df_mapper2(func, df)
3   %# small interface to iterate some vector func over the elements of a
4   %# dataframe. This one is specifically adapted to all functions where
5   %# the first argument, if numeric, is 'dim'.
6
7   %% Copyright (C) 2009-2012 Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
8   %%
9   %% This file is part of Octave.
10   %%
11   %% Octave is free software; you can redistribute it and/or
12   %% modify it under the terms of the GNU General Public
13   %% License as published by the Free Software Foundation;
14   %% either version 2, or (at your option) any later version.
15   %%
16   %% Octave is distributed in the hope that it will be useful,
17   %% but WITHOUT ANY WARRANTY; without even the implied
18   %% warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19   %% PURPOSE.  See the GNU General Public License for more
20   %% details.
21   %%
22   %% You should have received a copy of the GNU General Public
23   %% License along with Octave; see the file COPYING.  If not,
24   %% write to the Free Software Foundation, 51 Franklin Street -
25   %% Fifth Floor, Boston, MA 02110-1301, USA.
26   
27   %#
28   %# $Id: df_mapper2.m 9585 2012-02-05 15:32:46Z cdemills $
29   %#
30
31   dim = 1; resu = []; vout = varargin;
32   
33   %# take care of constructs as min(x, [], dim)  
34   if (!isempty(varargin)),
35     indk = 1; while indk <= length(varargin),
36       if (isnumeric(varargin{indk})),
37         if (isempty(varargin{indk})),
38           indk = indk + 1; continue;
39         endif
40         dim = varargin{indk}; 
41         %# the "third" dim is the second on stored data
42         if 3 == dim, vout(indk) = 2; endif
43       endif
44       break;
45     endwhile
46   endif
47
48   switch(dim)
49     case {1},
50       resu = df_colmeta(df);
51       for indi = 1:df._cnt(2),
52         resu._data{indi} = feval(func, df._data{indi}(:, df._rep{indi}), \
53                                  vout{:});
54         resu._rep{indi} = 1:size(resu._data{indi}, 2);
55       endfor
56       resu._cnt(1) = max(cellfun('size', resu._data, 1));
57       if (resu._cnt(1) == df._cnt(1)),
58         %# the func was not contracting
59         resu._ridx = df._ridx;
60         resu._name{1} = resu._name{1}; resu._over{1} = resu._over{1};
61       endif
62     case {2},
63       error('Operation not implemented');
64     case {3},
65       resu = df_allmeta(df); 
66       for indi = 1:df._cnt(2),
67         resu._data{indi} = feval(func, df._data{indi}(:, df._rep{indi}), \
68                                  vout{:});
69         resu._rep{indi} = 1:size(resu._data{indi}, 2);
70       endfor
71     otherwise
72       error("Invalid dimension %d", dim); 
73   endswitch
74
75   resu = df_thirddim(resu);
76
77 endfunction