7017
|
1 ## Copyright (C) 2003, 2007 Shai Ayal |
6257
|
2 ## |
6440
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
6257
|
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. |
6257
|
9 ## |
6440
|
10 ## Octave is distributed in the hope that it will be useful, but |
6257
|
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/>. |
6257
|
18 |
|
19 ## -*- texinfo -*- |
6590
|
20 ## @deftypefn {Function File} {[@var{c}, @var{lev}] =} contourc (@var{x}, @var{y}, @var{z}, @var{vn}) |
6257
|
21 ## Compute isolines (countour 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 |
6547
|
29 ## @var{c} = [lev1, x1, x2, ..., levn, x1, x2, ... |
|
30 ## len1, y1, y2, ..., lenn, y1, y2, ...] |
6257
|
31 ## @end example |
|
32 ## |
|
33 ## @noindent |
|
34 ## in which contour line @var{n} has a level (height) of @var{levn} and |
|
35 ## length of @var{lenn}. |
|
36 ## |
|
37 ## If @var{x} and @var{y} are omitted they are taken as the row/column |
|
38 ## index of @var{z}. @var{vn} is either a scalar denoting the number of lines |
|
39 ## to compute or a vector containing the values of the lines. If only one |
|
40 ## value is wanted, set @code{@var{vn} = [val, val]}; |
|
41 ## If @var{vn} is omitted it defaults to 10. |
|
42 ## |
6592
|
43 ## For example, |
6257
|
44 ## @example |
6592
|
45 ## @group |
|
46 ## x = 0:2; |
|
47 ## y = x; |
|
48 ## z = x' * y; |
|
49 ## contourc (x, y, z, 2:3) |
|
50 ## @result{} 2.0000 2.0000 1.0000 3.0000 1.5000 2.0000 |
|
51 ## 2.0000 1.0000 2.0000 2.0000 2.0000 1.5000 |
|
52 ## |
|
53 ## @end group |
6257
|
54 ## @end example |
|
55 ## @seealso{contour} |
|
56 ## @end deftypefn |
|
57 |
7327
|
58 ## Author: Shai Ayal <shaiay@users.sourceforge.net> |
6257
|
59 |
8236
|
60 function [cout, lev] = contourc (varargin) |
6257
|
61 |
|
62 if (nargin == 1) |
|
63 vn = 10; |
|
64 z = varargin{1}; |
7327
|
65 [nr, nc] = size (z); |
|
66 x = 1:nc; |
|
67 y = 1:nr; |
6257
|
68 elseif (nargin == 2) |
|
69 vn = varargin{2}; |
|
70 z = varargin{1}; |
7327
|
71 [nr, nc] = size (z); |
|
72 x = 1:nc; |
|
73 y = 1:nr; |
6257
|
74 elseif (nargin == 3) |
|
75 vn = 10; |
|
76 x = varargin{1}; |
|
77 y = varargin{2}; |
|
78 z = varargin{3}; |
|
79 elseif (nargin == 4) |
|
80 vn = varargin{4}; |
|
81 x = varargin{1}; |
|
82 y = varargin{2}; |
|
83 z = varargin{3}; |
|
84 else |
|
85 print_usage (); |
|
86 endif |
|
87 |
|
88 if (isscalar (vn)) |
|
89 vv = linspace (min (z(:)), max (z(:)), vn+2)(2:end-1); |
|
90 else |
|
91 vv = unique (sort (vn)); |
|
92 endif |
|
93 |
7327
|
94 if (isvector (x) && isvector (y)) |
|
95 c = __contourc__ (x(:)', y(:)', z, vv); |
|
96 else |
|
97 ## Indexes x,y for the purpose of __contourc__. |
|
98 ii = 1:size (z,2); |
|
99 jj = 1:size (z,1); |
|
100 |
|
101 ## Now call __contourc__ for the real work... |
|
102 c = __contourc__ (ii, jj, z, vv); |
|
103 |
|
104 ## Map the contour lines from index space (i,j) back |
|
105 ## to the original grid (x,y) |
|
106 i = 1; |
6257
|
107 |
7327
|
108 while (i < size (c,2)) |
|
109 clen = c(2, i); |
|
110 ind = i + [1 : clen]; |
6257
|
111 |
7327
|
112 ci = c(1, ind); |
|
113 cj = c(2,ind); |
|
114 |
|
115 ## due to rounding errors some elements of ci and cj |
|
116 ## can fall out of the range of ii and jj and interp2 would |
|
117 ## return NA for those values. |
|
118 ## The permitted range is enforced here: |
|
119 |
|
120 ci = max (ci, 1); ci = min (ci, size (z, 2)); |
|
121 cj = max (cj, 1); cj = min (cj, size (z, 1)); |
|
122 |
|
123 c(1, ind) = interp2 (ii, jj, x, ci, cj); |
|
124 c(2, ind) = interp2 (ii, jj, y, ci, cj); |
|
125 |
|
126 i = i + clen + 1; |
|
127 endwhile |
6257
|
128 endif |
7327
|
129 |
8236
|
130 if (nargout > 0) |
|
131 cout = c; |
6257
|
132 lev = vv; |
|
133 endif |
|
134 |
|
135 endfunction |
7245
|
136 |
|
137 %!demo |
|
138 %! x = 0:2; |
|
139 %! y = x; |
|
140 %! z = x' * y; |
|
141 %! contourc (x, y, z, 2:3) |