[9fe700]: inst / @dataframe / permute.m Maximize Restore History

Download this file

permute.m    116 lines (102 with data), 3.4 kB

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