Mercurial > hg > octave-lyh
annotate scripts/general/accumdim.m @ 16385:a1690c3e93eb
move hook_function constructor to .cc file
* hook-fcn.cc: New file. Move hook_function::hook_function definition
here from hook-fcn.h.
* libinterp/interpfcn/module.mk (INTERPFCN_SRC): Include hook-fcn.cc
in the list.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 28 Mar 2013 02:52:18 -0400 |
parents | f3b5cadfd6d5 |
children | 65546674d336 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
14119
diff
changeset
|
1 ## Copyright (C) 2010-2012 VZLU Prague |
10395 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7 ## the Free Software Foundation; either version 3 of the License, or (at | |
8 ## your option) any later version. | |
9 ## | |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
16 ## along with Octave; see the file COPYING. If not, see | |
17 ## <http://www.gnu.org/licenses/>. | |
18 | |
19 ## -*- texinfo -*- | |
10692
b32a0214a464
Use > 1 test to find first non-singleton dimension rather than != 1.
Rik <octave@nomad.inbox5.com>
parents:
10397
diff
changeset
|
20 ## @deftypefn {Function File} {} accumdim (@var{subs}, @var{vals}, @var{dim}, @var{n}, @var{func}, @var{fillval}) |
10395 | 21 ## Create an array by accumulating the slices of an array into the |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
22 ## positions defined by their subscripts along a specified dimension. |
10395 | 23 ## The subscripts are defined by the index vector @var{subs}. |
10711
fbd7843974fa
Periodic grammar check of documentation files to ensure common format.
Rik <octave@nomad.inbox5.com>
parents:
10692
diff
changeset
|
24 ## The dimension is specified by @var{dim}. If not given, it defaults |
14119
94e2a76f1e5a
doc: Final grammarcheck and spellcheck before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
14116
diff
changeset
|
25 ## to the first non-singleton dimension. The length of @var{subs} must |
14113
dac62c415e8b
Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12974
diff
changeset
|
26 ## be equal to @code{size (@var{vals}, @var{dim})}. |
10395 | 27 ## |
14113
dac62c415e8b
Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12974
diff
changeset
|
28 ## The extent of the result matrix in the working dimension will be |
14119
94e2a76f1e5a
doc: Final grammarcheck and spellcheck before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
14116
diff
changeset
|
29 ## determined by the subscripts themselves. However, if @var{n} is |
14113
dac62c415e8b
Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12974
diff
changeset
|
30 ## defined it determines this extent. |
10395 | 31 ## |
32 ## The default action of @code{accumdim} is to sum the subarrays with the | |
14113
dac62c415e8b
Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12974
diff
changeset
|
33 ## same subscripts. This behavior can be modified by defining the |
dac62c415e8b
Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12974
diff
changeset
|
34 ## @var{func} function. This should be a function or function handle |
dac62c415e8b
Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12974
diff
changeset
|
35 ## that accepts an array and a dimension, and reduces the array along |
dac62c415e8b
Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12974
diff
changeset
|
36 ## this dimension. As a special exception, the built-in @code{min} and |
dac62c415e8b
Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12974
diff
changeset
|
37 ## @code{max} functions can be used directly, and @code{accumdim} |
dac62c415e8b
Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12974
diff
changeset
|
38 ## accounts for the middle empty argument that is used in their calling. |
10395 | 39 ## |
14113
dac62c415e8b
Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12974
diff
changeset
|
40 ## The slices of the returned array that have no subscripts associated |
dac62c415e8b
Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12974
diff
changeset
|
41 ## with them are set to zero. Defining @var{fillval} to some other |
14119
94e2a76f1e5a
doc: Final grammarcheck and spellcheck before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
14116
diff
changeset
|
42 ## value allows these values to be defined. |
10395 | 43 ## |
10397
180931276a52
fix example in accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
10396
diff
changeset
|
44 ## An example of the use of @code{accumdim} is: |
10395 | 45 ## |
14116
951eacaf9381
Initial documentation for broadcasting and general vectorization guidelines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14113
diff
changeset
|
46 ## @example |
10395 | 47 ## @group |
14113
dac62c415e8b
Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12974
diff
changeset
|
48 ## accumdim ([1, 2, 1, 2, 1], [ 7, -10, 4; |
dac62c415e8b
Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12974
diff
changeset
|
49 ## -5, -12, 8; |
dac62c415e8b
Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12974
diff
changeset
|
50 ## -12, 2, 8; |
dac62c415e8b
Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12974
diff
changeset
|
51 ## -10, 9, -3; |
dac62c415e8b
Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12974
diff
changeset
|
52 ## -5, -3, -13]) |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
53 ## @result{} [-10,-11,-1;-15,-3,5] |
10395 | 54 ## @end group |
14116
951eacaf9381
Initial documentation for broadcasting and general vectorization guidelines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14113
diff
changeset
|
55 ## @end example |
10395 | 56 ## |
57 ## @seealso{accumarray} | |
58 ## @end deftypefn | |
59 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
60 function A = accumdim (subs, vals, dim, n = 0, func = [], fillval = 0) |
10395 | 61 |
62 if (nargin < 2 || nargin > 5) | |
63 print_usage (); | |
64 endif | |
65 | |
66 if (isempty (fillval)) | |
67 fillval = 0; | |
68 endif | |
69 | |
70 if (! isvector (subs)) | |
10692
b32a0214a464
Use > 1 test to find first non-singleton dimension rather than != 1.
Rik <octave@nomad.inbox5.com>
parents:
10397
diff
changeset
|
71 error ("accumdim: SUBS must be a subscript vector"); |
10395 | 72 elseif (! isindex (subs)) # creates index cache |
73 error ("accumdim: indices must be positive integers"); | |
74 else | |
75 m = max (subs); | |
14113
dac62c415e8b
Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12974
diff
changeset
|
76 if (n == 0 || isempty (n)) |
10395 | 77 n = m; |
78 elseif (n < m) | |
11589
b0084095098e
missing semicolons in script files
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
79 error ("accumdim: N index out of range"); |
10395 | 80 endif |
81 endif | |
82 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
83 sz = size (vals); |
10395 | 84 |
85 if (nargin < 3) | |
14221
b03b15d507d4
accumdim.m: Use common code idiom for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
86 ## Find the first non-singleton dimension. |
b03b15d507d4
accumdim.m: Use common code idiom for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
87 (dim = find (sz > 1, 1)) || (dim = 1); |
10396
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10395
diff
changeset
|
88 elseif (! isindex (dim)) |
10692
b32a0214a464
Use > 1 test to find first non-singleton dimension rather than != 1.
Rik <octave@nomad.inbox5.com>
parents:
10397
diff
changeset
|
89 error ("accumdim: DIM must be a valid dimension"); |
10396
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10395
diff
changeset
|
90 elseif (dim > length (sz)) |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10395
diff
changeset
|
91 sz(end+1:dim) = 1; |
10395 | 92 endif |
93 sz(dim) = n; | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
94 |
14113
dac62c415e8b
Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12974
diff
changeset
|
95 if (length (subs) != size (vals, dim)) |
15202
f3b5cadfd6d5
fix missing semicolons in various .m files
John W. Eaton <jwe@octave.org>
parents:
14363
diff
changeset
|
96 error ("accumdim: dimension mismatch"); |
14113
dac62c415e8b
Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12974
diff
changeset
|
97 endif |
dac62c415e8b
Do more error checking on accumarray and accumdim input.
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12974
diff
changeset
|
98 |
10396
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10395
diff
changeset
|
99 if (isempty (func) || func == @sum) |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10395
diff
changeset
|
100 ## Fast summation case. |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
101 A = __accumdim_sum__ (subs, vals, dim, n); |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
102 |
10396
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10395
diff
changeset
|
103 ## Fill in nonzero fill value |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10395
diff
changeset
|
104 if (fillval != 0) |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10395
diff
changeset
|
105 mask = true (n, 1); |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10395
diff
changeset
|
106 mask(subs) = false; |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10395
diff
changeset
|
107 subsc = {':'}(ones (1, length (sz))); |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10395
diff
changeset
|
108 subsc{dim} = mask; |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10395
diff
changeset
|
109 A(subsc{:}) = fillval; |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10395
diff
changeset
|
110 endif |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10395
diff
changeset
|
111 return |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10395
diff
changeset
|
112 endif |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10395
diff
changeset
|
113 |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10395
diff
changeset
|
114 ## The general case. |
10395 | 115 ns = length (subs); |
116 ## Sort indices. | |
117 [subs, idx] = sort (subs(:)); | |
118 ## Identify runs. | |
119 jdx = find (subs(1:ns-1) != subs(2:ns)); | |
120 jdx = [jdx; ns]; | |
121 ## Collect common slices. | |
122 szc = num2cell (sz); | |
123 szc{dim} = diff ([0; jdx]); | |
124 subsc = {':'}(ones (1, length (sz))); | |
125 subsc{dim} = idx; | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
126 vals = mat2cell (vals(subsc{:}), szc{:}); |
10395 | 127 ## Apply reductions. Special case min, max. |
128 if (func == @min || func == @max) | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
129 vals = cellfun (func, vals, {[]}, {dim}, "uniformoutput", false); |
10395 | 130 else |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
131 vals = cellfun (func, vals, {dim}, "uniformoutput", false); |
10395 | 132 endif |
133 subs = subs(jdx); | |
134 | |
135 ## Concatenate reduced slices. | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
136 vals = cat (dim, vals{:}); |
10395 | 137 |
138 ## Construct matrix of fillvals. | |
139 if (fillval == 0) | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
140 A = zeros (sz, class (vals)); |
10395 | 141 else |
142 A = repmat (fillval, sz); | |
143 endif | |
144 | |
145 ## Set the reduced values. | |
146 subsc{dim} = subs; | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
147 A(subsc{:}) = vals; |
10395 | 148 |
149 endfunction | |
150 | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14335
diff
changeset
|
151 |
10395 | 152 %%test accumdim vs. accumarray |
153 | |
154 %!shared a | |
155 %! a = rand (5, 5, 5); | |
156 | |
157 %!assert (accumdim ([1;3;1;3;3], a)(:,2,3), accumarray ([1;3;1;3;3], a(:,2,3))) | |
158 %!assert (accumdim ([2;3;2;2;2], a, 2, 4)(4,:,2), accumarray ([2;3;2;2;2], a(4,:,2), [1,4])) | |
159 %!assert (accumdim ([2;3;2;1;2], a, 3, 3, @min)(1,5,:), accumarray ([2;3;2;1;2], a(1,5,:), [1,1,3], @min)) | |
160 %!assert (accumdim ([1;3;2;2;1], a, 2, 3, @median)(4,:,5), accumarray ([1;3;2;2;1], a(4,:,5), [1,3], @median)) | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14335
diff
changeset
|
161 |