Mercurial > hg > octave-nkf
annotate scripts/general/accumdim.m @ 12541:dd2c70b30f28
Add tests for ifftshift.m
author | Robert T. Short <octave@phaselockedsystems.com.com> |
---|---|
date | Sat, 26 Mar 2011 06:50:12 -0700 |
parents | b0084095098e |
children | 6590446c2498 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 2010-2011 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 |
10395 | 25 ## to the first non-singleton dimension. |
26 ## | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
27 ## The extent of the result matrix in the working dimension will be determined |
10395 | 28 ## by the subscripts themselves. |
29 ## However, if @var{n} is defined it determines this extent. | |
30 ## | |
31 ## The default action of @code{accumdim} is to sum the subarrays with the | |
32 ## same subscripts. This behavior can be modified by defining the @var{func} | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
33 ## function. This should be a function or function handle that accepts an |
10395 | 34 ## array and a dimension, and reduces the array along this dimension. |
35 ## As a special exception, the built-in @code{min} and @code{max} functions | |
36 ## can be used directly, and @code{accumdim} accounts for the middle empty | |
37 ## argument that is used in their calling. | |
38 ## | |
39 ## The slices of the returned array that have no subscripts associated with | |
40 ## them are set to zero. Defining @var{fillval} to some other value allows | |
41 ## these values to be defined. | |
42 ## | |
10397
180931276a52
fix example in accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
10396
diff
changeset
|
43 ## An example of the use of @code{accumdim} is: |
10395 | 44 ## |
45 ## @example | |
46 ## @group | |
10397
180931276a52
fix example in accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
10396
diff
changeset
|
47 ## accumdim ([1, 2, 1, 2, 1], [7,-10,4;-5,-12,8;-12,2,8;-10,9,-3;-5,-3,-13]) |
180931276a52
fix example in accumdim
Jaroslav Hajek <highegg@gmail.com>
parents:
10396
diff
changeset
|
48 ## @result{} ans = [-10,-11,-1;-15,-3,5] |
10395 | 49 ## @end group |
50 ## @end example | |
51 ## | |
52 ## @seealso{accumarray} | |
53 ## @end deftypefn | |
54 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
55 function A = accumdim (subs, vals, dim, n = 0, func = [], fillval = 0) |
10395 | 56 |
57 if (nargin < 2 || nargin > 5) | |
58 print_usage (); | |
59 endif | |
60 | |
61 if (isempty (fillval)) | |
62 fillval = 0; | |
63 endif | |
64 | |
65 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
|
66 error ("accumdim: SUBS must be a subscript vector"); |
10395 | 67 elseif (! isindex (subs)) # creates index cache |
68 error ("accumdim: indices must be positive integers"); | |
69 else | |
70 m = max (subs); | |
71 if (n == 0) | |
72 n = m; | |
73 elseif (n < m) | |
11589
b0084095098e
missing semicolons in script files
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
74 error ("accumdim: N index out of range"); |
10395 | 75 endif |
76 endif | |
77 | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
78 sz = size (vals); |
10395 | 79 |
80 if (nargin < 3) | |
10790
01f1643dfbb1
fix flipdim with trailing singleton dims
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
81 [~, dim] = max (sz != 1); # first non-singleton dim |
10396
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10395
diff
changeset
|
82 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
|
83 error ("accumdim: DIM must be a valid dimension"); |
10396
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10395
diff
changeset
|
84 elseif (dim > length (sz)) |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10395
diff
changeset
|
85 sz(end+1:dim) = 1; |
10395 | 86 endif |
87 sz(dim) = n; | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
88 |
10396
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10395
diff
changeset
|
89 if (isempty (func) || func == @sum) |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10395
diff
changeset
|
90 ## 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
|
91 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
|
92 |
10396
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10395
diff
changeset
|
93 ## Fill in nonzero fill value |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10395
diff
changeset
|
94 if (fillval != 0) |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10395
diff
changeset
|
95 mask = true (n, 1); |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10395
diff
changeset
|
96 mask(subs) = false; |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10395
diff
changeset
|
97 subsc = {':'}(ones (1, length (sz))); |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10395
diff
changeset
|
98 subsc{dim} = mask; |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10395
diff
changeset
|
99 A(subsc{:}) = fillval; |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10395
diff
changeset
|
100 endif |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10395
diff
changeset
|
101 return |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10395
diff
changeset
|
102 endif |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10395
diff
changeset
|
103 |
a0b51ac0f88a
optimize accumdim with summation
Jaroslav Hajek <highegg@gmail.com>
parents:
10395
diff
changeset
|
104 ## The general case. |
10395 | 105 ns = length (subs); |
106 ## Sort indices. | |
107 [subs, idx] = sort (subs(:)); | |
108 ## Identify runs. | |
109 jdx = find (subs(1:ns-1) != subs(2:ns)); | |
110 jdx = [jdx; ns]; | |
111 ## Collect common slices. | |
112 szc = num2cell (sz); | |
113 szc{dim} = diff ([0; jdx]); | |
114 subsc = {':'}(ones (1, length (sz))); | |
115 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
|
116 vals = mat2cell (vals(subsc{:}), szc{:}); |
10395 | 117 ## Apply reductions. Special case min, max. |
118 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
|
119 vals = cellfun (func, vals, {[]}, {dim}, "uniformoutput", false); |
10395 | 120 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
|
121 vals = cellfun (func, vals, {dim}, "uniformoutput", false); |
10395 | 122 endif |
123 subs = subs(jdx); | |
124 | |
125 ## 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
|
126 vals = cat (dim, vals{:}); |
10395 | 127 |
128 ## Construct matrix of fillvals. | |
129 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
|
130 A = zeros (sz, class (vals)); |
10395 | 131 else |
132 A = repmat (fillval, sz); | |
133 endif | |
134 | |
135 ## Set the reduced values. | |
136 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
|
137 A(subsc{:}) = vals; |
10395 | 138 |
139 endfunction | |
140 | |
141 %%test accumdim vs. accumarray | |
142 | |
143 %!shared a | |
144 %! a = rand (5, 5, 5); | |
145 | |
146 %!assert (accumdim ([1;3;1;3;3], a)(:,2,3), accumarray ([1;3;1;3;3], a(:,2,3))) | |
147 %!assert (accumdim ([2;3;2;2;2], a, 2, 4)(4,:,2), accumarray ([2;3;2;2;2], a(4,:,2), [1,4])) | |
148 %!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)) | |
149 %!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)) |