]> Creatis software - CreaPhase.git/blob - octave_packages/dataframe-0.9.1/@dataframe/permute.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / dataframe-0.9.1 / @dataframe / permute.m
1 function resu = permute(df, perm) 
2   
3   %% Copyright (C) 2009-2012 Pascal Dupuis <Pascal.Dupuis@uclouvain.be>
4   %%
5   %% This file is part of Octave.
6   %%
7   %% Octave is free software; you can redistribute it and/or
8   %% modify it under the terms of the GNU General Public
9   %% License as published by the Free Software Foundation;
10   %% either version 2, or (at your option) any later version.
11   %%
12   %% Octave is distributed in the hope that it will be useful,
13   %% but WITHOUT ANY WARRANTY; without even the implied
14   %% warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15   %% PURPOSE.  See the GNU General Public License for more
16   %% details.
17   %%
18   %% You should have received a copy of the GNU General Public
19   %% License along with Octave; see the file COPYING.  If not,
20   %% write to the Free Software Foundation, 51 Franklin Street -
21   %% Fifth Floor, Boston, MA 02110-1301, USA.
22
23   %#
24   %# $Id: permute.m 9585 2012-02-05 15:32:46Z cdemills $
25   %#
26
27   resu = dataframe([]);
28
29   if (length(df._cnt) >= length(perm)),
30     resu._cnt = df._cnt(perm);
31   else
32     resu._cnt = [df._cnt 1](perm);
33   endif
34   
35   if (ndims(df._ridx) < 3),
36     resu._ridx = permute(df._ridx, [min(perm(1), 2) min(perm(2:end))]);
37   else
38     resu._ridx = permute(df._ridx, perm);
39   endif
40
41   if (size(resu._ridx, 1) < resu._cnt(1)),
42     %# adjust index size, if required
43     resu._ridx(end+1:resu._cnt(1), :, :) = NA;
44   endif
45
46   if (2 == perm(1)),
47     resu._name{1} = df._name{2};
48     resu._over{1} = df._over{2};
49     indc = length(resu._name{1});
50     indi = resu._cnt(1) - indc;
51     if (indi > 0),
52       %# generate a name for the new row(s)
53       dummy = cstrcat(repmat('_', indi, 1), ...
54                       strjust(num2str(indc + (1:indi).'), 'left'));
55       resu._name{1}(indc + (1:indi)) = cellstr(dummy);
56       resu._over{1}(1, indc + (1:indi)) = true;
57     endif 
58   else
59     resu._name{1} = df._name{1};
60     resu._over{1} = df._over{1};
61   endif
62
63   
64   if (2 == perm(2)),
65     resu._name{2} = df._name{2};
66     resu._over{2} = df._over{2};
67   else
68     resu._name{2} = df._name{1};
69     resu._over{2} = df._over{1};
70   endif
71   
72   if (isempty(resu._name{2})),
73     indc = 0;
74   else
75     indc = length(resu._name{2});
76   endif
77   indi = resu._cnt(2) - indc;
78   if (indi > 0),
79     %# generate a name for the new column(s)
80     dummy = cstrcat(repmat('_', indi, 1), ...
81                     strjust(num2str(indc + (1:indi).'), 'left'));
82     resu._name{2}(indc + (1:indi)) = cellstr(dummy);
83     resu._over{2}(1, indc + (1:indi)) = true;    
84   endif 
85   
86   if (2 != perm(2)),
87     %# recompute the new type
88     dummy = zeros(0, class(sum(cellfun(@(x) zeros(1, class(x(1))),\
89                                        df._data))));
90     resu._type(1:resu._cnt(2)) = class(dummy);
91     dummy = permute(df_whole(df), perm);
92     for indi = 1:resu._cnt(2),
93       resu._data{indi} = squeeze(dummy(:, indi, :));
94       resu._rep{indi} = 1:size(resu._data{indi}, 2);
95     endfor 
96   else %# 2 == perm(2)
97     if (1 == perm(1)), %# blank operation
98       resu._type = df._type;
99       resu._data = df._data;
100       resu._rep = df._rep;
101     else
102       for indi = 1:resu._cnt(2),
103         unfolded = df._data{indi}(:, df._rep{indi});
104         resu._data{indi} = permute(unfolded, [2 1]);
105         resu._rep{indi} = 1:size(resu._data{indi}, 2);
106         resu._type{indi} = df._type{indi};
107       endfor    
108     endif
109   endif
110
111   resu._src = df._src;
112   resu._cmt = df._cmt;
113
114 endfunction