changelog shortlog tags changeset files revisions annotate raw

scripts/plot/contourc.m

changeset 10289: 4b124317dc38
parent:1bf0ce0930be
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (69 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1## Copyright (C) 2003, 2007, 2008, 2009 Shai Ayal
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 -*-
20## @deftypefn {Function File} {[@var{c}, @var{lev}] =} contourc (@var{x}, @var{y}, @var{z}, @var{vn})
21## Compute isolines (contour lines) of the matrix @var{z}.
22## Parameters @var{x}, @var{y} and @var{vn} are optional.
23##
24## The return value @var{lev} is a vector of the contour levels.
25## The return value @var{c} is a 2 by @var{n} matrix containing the
26## contour lines in the following format
27##
28## @example
29## @group
30## @var{c} = [lev1, x1, x2, @dots{}, levn, x1, x2, ...
31## len1, y1, y2, @dots{}, lenn, y1, y2, @dots{}]
32## @end group
33## @end example
34##
35## @noindent
36## in which contour line @var{n} has a level (height) of @var{levn} and
37## length of @var{lenn}.
38##
39## If @var{x} and @var{y} are omitted they are taken as the row/column
40## index of @var{z}. @var{vn} is either a scalar denoting the number of lines
41## to compute or a vector containing the values of the lines. If only one
42## value is wanted, set @code{@var{vn} = [val, val]};
43## If @var{vn} is omitted it defaults to 10.
44##
45## For example,
46## @example
47## @group
48## x = 0:2;
49## y = x;
50## z = x' * y;
51## contourc (x, y, z, 2:3)
52## @result{} 2.0000 2.0000 1.0000 3.0000 1.5000 2.0000
53## 2.0000 1.0000 2.0000 2.0000 2.0000 1.5000
54##
55## @end group
56## @end example
57## @seealso{contour}
58## @end deftypefn
59
60## Author: Shai Ayal <shaiay@users.sourceforge.net>
61
62function [cout, lev] = contourc (varargin)
63
64 if (nargin == 1)
65 vn = 10;
66 z = varargin{1};
67 [nr, nc] = size (z);
68 x = 1:nc;
69 y = 1:nr;
70 elseif (nargin == 2)
71 vn = varargin{2};
72 z = varargin{1};
73 [nr, nc] = size (z);
74 x = 1:nc;
75 y = 1:nr;
76 elseif (nargin == 3)
77 vn = 10;
78 x = varargin{1};
79 y = varargin{2};
80 z = varargin{3};
81 elseif (nargin == 4)
82 vn = varargin{4};
83 x = varargin{1};
84 y = varargin{2};
85 z = varargin{3};
86 else
87 print_usage ();
88 endif
89
90 if (isscalar (vn))
91 vv = linspace (min (z(:)), max (z(:)), vn+2)(2:end-1);
92 else
93 vv = unique (sort (vn));
94 endif
95
96 if (isvector (x) && isvector (y))
97 c = __contourc__ (x(:)', y(:)', z, vv);
98 else
99 ## Indexes x,y for the purpose of __contourc__.
100 ii = 1:size (z,2);
101 jj = 1:size (z,1);
102
103 ## Now call __contourc__ for the real work...
104 c = __contourc__ (ii, jj, z, vv);
105
106 ## Map the contour lines from index space (i,j) back
107 ## to the original grid (x,y)
108 i = 1;
109
110 while (i < size (c,2))
111 clen = c(2, i);
112 ind = i + [1 : clen];
113
114 ci = c(1, ind);
115 cj = c(2,ind);
116
117 ## due to rounding errors some elements of ci and cj
118 ## can fall out of the range of ii and jj and interp2 would
119 ## return NA for those values.
120 ## The permitted range is enforced here:
121
122 ci = max (ci, 1); ci = min (ci, size (z, 2));
123 cj = max (cj, 1); cj = min (cj, size (z, 1));
124
125 c(1, ind) = interp2 (ii, jj, x, ci, cj);
126 c(2, ind) = interp2 (ii, jj, y, ci, cj);
127
128 i = i + clen + 1;
129 endwhile
130 endif
131
132 if (nargout > 0)
133 cout = c;
134 lev = vv;
135 endif
136
137endfunction
138
139%!test
140%! x = 0:2;
141%! y = x;
142%! z = x' * y;
143%! [c_actual, lev_actual]= contourc (x, y, z, 2:3);
144%! c_expected = [2, 1, 1, 2, 2, 3, 1.5, 2; 4, 2, 2, 1, 1, 2, 2, 1.5];
145%! lev_expected = [2 3];
146%! assert (c_actual, c_expected, eps)
147%! assert (lev_actual, lev_expected, eps)
148
149