Mercurial > hg > octave-nkf
annotate scripts/general/shiftdim.m @ 18754:0ede4dbb37f1
Overhaul interp1, interp2, interp3 functions.
* NEWS: Announce change in 'cubic' interpolation method for interp2
to match Matlab.
* bicubic.m: Use interp2 (..., "spline") in %!tests.
* interp1.m: Improve docstring. Use switch statement instead of if/elseif tree
for simpler code. Use more informative error message than 'table too short'.
Add titles to demo plots. Add new demo block showing difference between 'pchip'
and 'spline' methods.
* interp2.m: Rewrite docstring. Use variable 'extrap' instead of 'extrapval' to
match documentation. Use clearer messages in error() calls. Make 'cubic' use
the same algorithm as 'pchip' for Matlab compatibility. Use Octave coding
conventions regarding spaces between variable and parenthesis. Added input
validation tests.
* interp3.m: Rewrite docstring. Use clearer messages in error() calls. Make
'cubic' use the same algorithm as 'pchip' for Matlab compatibility. Simplify
input processing. Rewrite some %!tests for clarity. Added input validation
tests.
author | Rik <rik@octave.org> |
---|---|
date | Sun, 30 Mar 2014 14:18:43 -0700 |
parents | d63878346099 |
children | 4197fc428c7d |
rev | line source |
---|---|
17744
d63878346099
maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents:
14363
diff
changeset
|
1 ## Copyright (C) 2004-2013 John Eaton and David Bateman |
4894 | 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
4894 | 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 | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
4894 | 18 |
19 ## -*- texinfo -*- | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
20 ## @deftypefn {Function File} {@var{y} =} shiftdim (@var{x}, @var{n}) |
6547 | 21 ## @deftypefnx {Function File} {[@var{y}, @var{ns}] =} shiftdim (@var{x}) |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
12575
diff
changeset
|
22 ## Shift the dimensions of @var{x} by @var{n}, where @var{n} must be |
9041
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
7017
diff
changeset
|
23 ## an integer scalar. When @var{n} is positive, the dimensions of |
4894 | 24 ## @var{x} are shifted to the left, with the leading dimensions |
9041
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
7017
diff
changeset
|
25 ## circulated to the end. If @var{n} is negative, then the dimensions |
5539 | 26 ## of @var{x} are shifted to the right, with @var{n} leading singleton |
4894 | 27 ## dimensions added. |
28 ## | |
29 ## Called with a single argument, @code{shiftdim}, removes the leading | |
30 ## singleton dimensions, returning the number of dimensions removed | |
31 ## in the second output argument @var{ns}. | |
32 ## | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
33 ## For example: |
4894 | 34 ## |
35 ## @example | |
36 ## @group | |
37 ## x = ones (1, 2, 3); | |
38 ## size (shiftdim (x, -1)) | |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
39 ## @result{} [1, 1, 2, 3] |
4894 | 40 ## size (shiftdim (x, 1)) |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
41 ## @result{} [2, 3] |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
42 ## [b, ns] = shiftdim (x) |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
43 ## @result{} b = [1, 1, 1; 1, 1, 1] |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
44 ## @result{} ns = 1 |
4894 | 45 ## @end group |
46 ## @end example | |
5642 | 47 ## @seealso {reshape, permute, ipermute, circshift, squeeze} |
4894 | 48 ## @end deftypefn |
49 | |
50 function [y, ns] = shiftdim (x, n) | |
51 | |
5518 | 52 if (nargin < 1 || nargin > 2) |
6046 | 53 print_usage (); |
5518 | 54 endif |
55 | |
56 nd = ndims (x); | |
57 orig_dims = size (x); | |
58 | |
4894 | 59 if (nargin == 1) |
12716
6835bc816ef2
shiftdim.m: Use common idiom for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
60 ## Find the first non-singleton dimension. |
6835bc816ef2
shiftdim.m: Use common idiom for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
61 (n = find (orig_dims != 1, 1) - 1) || (n = nd); |
6835bc816ef2
shiftdim.m: Use common idiom for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
62 elseif (! (isscalar (n) && n == fix (n))) |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
63 error ("shiftdim: N must be a scalar integer"); |
5518 | 64 endif |
65 | |
66 if (n >= nd) | |
67 n = rem (n, nd); | |
68 endif | |
69 | |
70 if (n < 0) | |
71 singleton_dims = ones (1, -n); | |
72 y = reshape (x, [singleton_dims, orig_dims]); | |
73 elseif (n > 0) | |
5984 | 74 ## We need permute here instead of reshape to shift values in a |
75 ## compatible way. | |
12716
6835bc816ef2
shiftdim.m: Use common idiom for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
76 y = permute (x, [n+1:nd 1:n]); |
4894 | 77 else |
5518 | 78 y = x; |
4894 | 79 endif |
5518 | 80 |
81 ns = n; | |
82 | |
4894 | 83 endfunction |
12716
6835bc816ef2
shiftdim.m: Use common idiom for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
84 |
6835bc816ef2
shiftdim.m: Use common idiom for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
85 |
6835bc816ef2
shiftdim.m: Use common idiom for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
86 %!test |
6835bc816ef2
shiftdim.m: Use common idiom for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
87 %! x = rand (1, 1, 4, 2); |
6835bc816ef2
shiftdim.m: Use common idiom for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
88 %! [y, ns] = shiftdim (x); |
6835bc816ef2
shiftdim.m: Use common idiom for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
89 %! assert (size (y), [4 2]); |
6835bc816ef2
shiftdim.m: Use common idiom for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
90 %! assert (ns, 2); |
6835bc816ef2
shiftdim.m: Use common idiom for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
91 %! assert (shiftdim (y, -2), x); |
6835bc816ef2
shiftdim.m: Use common idiom for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
92 %! assert (size (shiftdim (x, 2)), [4 2]); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
93 |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
94 %!assert (size (shiftdim (rand (0, 1, 2))), [0 1 2]) |
12716
6835bc816ef2
shiftdim.m: Use common idiom for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
95 |
6835bc816ef2
shiftdim.m: Use common idiom for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
96 %% Test input validation |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
97 %!error (shiftdim ()) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
98 %!error (shiftdim (1,2,3)) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
99 %!error (shiftdim (1, ones (2))) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
100 %!error (shiftdim (1, 1.5)) |
12716
6835bc816ef2
shiftdim.m: Use common idiom for finding first non-singleton dimension.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
101 |