Mercurial > octave-nkf
comparison scripts/general/interpn.m @ 16891:486c3e2731ff
interpn.m: Use Octave coding conventions.
* scripts/general/interpn.m: Use Octave coding conventions.
author | Rik <rik@octave.org> |
---|---|
date | Wed, 03 Jul 2013 13:48:49 -0700 |
parents | 5d3a684236b0 |
children | bc924baa2c4e |
comparison
equal
deleted
inserted
replaced
16890:830f27544bb7 | 16891:486c3e2731ff |
---|---|
74 print_usage (); | 74 print_usage (); |
75 endif | 75 endif |
76 | 76 |
77 if (ischar (varargin{end})) | 77 if (ischar (varargin{end})) |
78 method = varargin{end}; | 78 method = varargin{end}; |
79 nargs = nargs - 1; | 79 nargs -= 1; |
80 elseif (nargs > 1 && ischar (varargin{end - 1})) | 80 elseif (nargs > 1 && ischar (varargin{end - 1})) |
81 if (! isnumeric (varargin{end}) || ! isscalar (varargin{end})) | 81 if (! isnumeric (varargin{end}) || ! isscalar (varargin{end})) |
82 error ("interpn: extrapal is expected to be a numeric scalar"); | 82 error ("interpn: extrapal is expected to be a numeric scalar"); |
83 endif | 83 endif |
84 method = varargin{end - 1}; | 84 method = varargin{end - 1}; |
85 extrapval = varargin{end}; | 85 extrapval = varargin{end}; |
86 nargs = nargs - 2; | 86 nargs -= 2; |
87 endif | 87 endif |
88 | 88 |
89 if (nargs < 3) | 89 if (nargs < 3) |
90 v = varargin{1}; | 90 v = varargin{1}; |
91 m = 1; | 91 m = 1; |
100 endif | 100 endif |
101 sz = size (v); | 101 sz = size (v); |
102 nd = ndims (v); | 102 nd = ndims (v); |
103 x = cell (1, nd); | 103 x = cell (1, nd); |
104 y = cell (1, nd); | 104 y = cell (1, nd); |
105 for i = 1 : nd; | 105 for i = 1 : nd |
106 x{i} = 1 : sz(i); | 106 x{i} = 1 : sz(i); |
107 y{i} = 1 : (1 / (2 ^ m)) : sz(i); | 107 y{i} = 1 : (1 / (2 ^ m)) : sz(i); |
108 endfor | 108 endfor |
109 y{1} = y{1}.'; | 109 y{1} = y{1}.'; |
110 [y{:}] = ndgrid (y{:}); | 110 [y{:}] = ndgrid (y{:}); |
111 elseif (! isvector (varargin{1}) && nargs == (ndims (varargin{1}) + 1)) | 111 elseif (! isvector (varargin{1}) && nargs == (ndims (varargin{1}) + 1)) |
112 v = varargin{1}; | 112 v = varargin{1}; |
113 sz = size (v); | 113 sz = size (v); |
114 nd = ndims (v); | 114 nd = ndims (v); |
115 x = cell (1, nd); | 115 x = cell (1, nd); |
116 y = varargin (2 : nargs); | 116 y = varargin(2 : nargs); |
117 for i = 1 : nd; | 117 for i = 1 : nd |
118 x{i} = 1 : sz(i); | 118 x{i} = 1 : sz(i); |
119 endfor | 119 endfor |
120 elseif (rem (nargs, 2) == 1 && nargs == | 120 elseif (rem (nargs, 2) == 1 |
121 (2 * ndims (varargin{ceil (nargs / 2)})) + 1) | 121 && nargs == (2 * ndims (varargin{ceil (nargs / 2)})) + 1) |
122 nv = ceil (nargs / 2); | 122 nv = ceil (nargs / 2); |
123 v = varargin{nv}; | 123 v = varargin{nv}; |
124 sz = size (v); | 124 sz = size (v); |
125 nd = ndims (v); | 125 nd = ndims (v); |
126 x = varargin (1 : (nv - 1)); | 126 x = varargin(1 : (nv - 1)); |
127 y = varargin ((nv + 1) : nargs); | 127 y = varargin((nv + 1) : nargs); |
128 else | 128 else |
129 error ("interpn: wrong number or incorrectly formatted input arguments"); | 129 error ("interpn: wrong number or incorrectly formatted input arguments"); |
130 endif | 130 endif |
131 | 131 |
132 if (any (! cellfun ("isvector", x))) | 132 if (any (! cellfun ("isvector", x))) |
133 for i = 2 : nd | 133 for i = 2 : nd |
134 if (! size_equal (x{1}, x{i}) || ! size_equal (x{i}, v)) | 134 if (! size_equal (x{1}, x{i}) || ! size_equal (x{i}, v)) |
135 error ("interpn: dimensional mismatch"); | 135 error ("interpn: dimensional mismatch"); |
136 endif | 136 endif |
137 idx (1 : nd) = {1}; | 137 idx(1 : nd) = {1}; |
138 idx (i) = ":"; | 138 idx(i) = ":"; |
139 x{i} = x{i}(idx{:})(:); | 139 x{i} = x{i}(idx{:})(:); |
140 endfor | 140 endfor |
141 idx (1 : nd) = {1}; | 141 idx(1 : nd) = {1}; |
142 idx (1) = ":"; | 142 idx(1) = ":"; |
143 x{1} = x{1}(idx{:})(:); | 143 x{1} = x{1}(idx{:})(:); |
144 endif | 144 endif |
145 | 145 |
146 method = tolower (method); | 146 method = tolower (method); |
147 | 147 |
152 y = {foobar.y}; | 152 y = {foobar.y}; |
153 endif | 153 endif |
154 | 154 |
155 if (strcmp (method, "linear")) | 155 if (strcmp (method, "linear")) |
156 vi = __lin_interpn__ (x{:}, v, y{:}); | 156 vi = __lin_interpn__ (x{:}, v, y{:}); |
157 vi (isna (vi)) = extrapval; | 157 vi(isna (vi)) = extrapval; |
158 elseif (strcmp (method, "nearest")) | 158 elseif (strcmp (method, "nearest")) |
159 yshape = size (y{1}); | 159 yshape = size (y{1}); |
160 yidx = cell (1, nd); | 160 yidx = cell (1, nd); |
161 for i = 1 : nd | 161 for i = 1 : nd |
162 y{i} = y{i}(:); | 162 y{i} = y{i}(:); |
164 endfor | 164 endfor |
165 idx = cell (1,nd); | 165 idx = cell (1,nd); |
166 for i = 1 : nd | 166 for i = 1 : nd |
167 idx{i} = yidx{i} + (y{i} - x{i}(yidx{i})(:) >= x{i}(yidx{i} + 1)(:) - y{i}); | 167 idx{i} = yidx{i} + (y{i} - x{i}(yidx{i})(:) >= x{i}(yidx{i} + 1)(:) - y{i}); |
168 endfor | 168 endfor |
169 vi = v (sub2ind (sz, idx{:})); | 169 vi = v(sub2ind (sz, idx{:})); |
170 idx = zeros (prod (yshape), 1); | 170 idx = zeros (prod (yshape), 1); |
171 for i = 1 : nd | 171 for i = 1 : nd |
172 idx |= y{i} < min (x{i}(:)) | y{i} > max (x{i}(:)); | 172 idx |= y{i} < min (x{i}(:)) | y{i} > max (x{i}(:)); |
173 endfor | 173 endfor |
174 vi(idx) = extrapval; | 174 vi(idx) = extrapval; |
177 if (any (! cellfun ("isvector", y))) | 177 if (any (! cellfun ("isvector", y))) |
178 for i = 2 : nd | 178 for i = 2 : nd |
179 if (! size_equal (y{1}, y{i})) | 179 if (! size_equal (y{1}, y{i})) |
180 error ("interpn: dimensional mismatch"); | 180 error ("interpn: dimensional mismatch"); |
181 endif | 181 endif |
182 idx (1 : nd) = {1}; | 182 idx(1 : nd) = {1}; |
183 idx (i) = ":"; | 183 idx(i) = ":"; |
184 y{i} = y{i}(idx{:}); | 184 y{i} = y{i}(idx{:}); |
185 endfor | 185 endfor |
186 idx (1 : nd) = {1}; | 186 idx(1 : nd) = {1}; |
187 idx (1) = ":"; | 187 idx(1) = ":"; |
188 y{1} = y{1}(idx{:}); | 188 y{1} = y{1}(idx{:}); |
189 endif | 189 endif |
190 | 190 |
191 vi = __splinen__ (x, v, y, extrapval, "interpn"); | 191 vi = __splinen__ (x, v, y, extrapval, "interpn"); |
192 | 192 |
194 ly = length (y{1}); | 194 ly = length (y{1}); |
195 idx = cell (1, ly); | 195 idx = cell (1, ly); |
196 q = cell (1, nd); | 196 q = cell (1, nd); |
197 for i = 1 : ly | 197 for i = 1 : ly |
198 q(:) = i; | 198 q(:) = i; |
199 idx {i} = q; | 199 idx{i} = q; |
200 endfor | 200 endfor |
201 vi = vi (cellfun (@(x) sub2ind (size (vi), x{:}), idx)); | 201 vi = vi(cellfun (@(x) sub2ind (size (vi), x{:}), idx)); |
202 vi = reshape (vi, size (y{1})); | 202 vi = reshape (vi, size (y{1})); |
203 endif | 203 endif |
204 elseif (strcmp (method, "cubic")) | 204 elseif (strcmp (method, "cubic")) |
205 error ("interpn: cubic interpolation not yet implemented"); | 205 error ("interpn: cubic interpolation not yet implemented"); |
206 else | 206 else |