comparison scripts/general/interpn.m @ 14237:11949c9795a0

Revamp %!demos in m-files to use Octave coding conventions on spacing, etc. Add clf() to all demos using plot features to get reproducibility. Use 64 as input to all colormaps (jet (64)) to get reproducibility. * bicubic.m, cell2mat.m, celldisp.m, cplxpair.m, interp1.m, interp2.m, interpft.m, interpn.m, profile.m, profshow.m, convhull.m, delaunay.m, griddata.m, inpolygon.m, voronoi.m, autumn.m, bone.m, contrast.m, cool.m, copper.m, flag.m, gmap40.m, gray.m, hot.m, hsv.m, image.m, imshow.m, jet.m, ocean.m, pink.m, prism.m, rainbow.m, spring.m, summer.m, white.m, winter.m, condest.m, onenormest.m, axis.m, clabel.m, colorbar.m, comet.m, comet3.m, compass.m, contour.m, contour3.m, contourf.m, cylinder.m, daspect.m, ellipsoid.m, errorbar.m, ezcontour.m, ezcontourf.m, ezmesh.m, ezmeshc.m, ezplot.m, ezplot3.m, ezpolar.m, ezsurf.m, ezsurfc.m, feather.m, fill.m, fplot.m, grid.m, hold.m, isosurface.m, legend.m, loglog.m, loglogerr.m, pareto.m, patch.m, pbaspect.m, pcolor.m, pie.m, pie3.m, plot3.m, plotmatrix.m, plotyy.m, polar.m, quiver.m, quiver3.m, rectangle.m, refreshdata.m, ribbon.m, rose.m, scatter.m, scatter3.m, semilogx.m, semilogxerr.m, semilogy.m, semilogyerr.m, shading.m, slice.m, sombrero.m, stairs.m, stem.m, stem3.m, subplot.m, surf.m, surfc.m, surfl.m, surfnorm.m, text.m, title.m, trimesh.m, triplot.m, trisurf.m, uigetdir.m, uigetfile.m, uimenu.m, uiputfile.m, waitbar.m, xlim.m, ylim.m, zlim.m, mkpp.m, pchip.m, polyaffine.m, spline.m, bicgstab.m, cgs.m, gplot.m, pcg.m, pcr.m, treeplot.m, strtok.m, demo.m, example.m, rundemos.m, speed.m, test.m, calendar.m, datestr.m, datetick.m, weekday.m: Revamp %!demos to use Octave coding conventions on spacing, etc.
author Rik <octave@nomad.inbox5.com>
date Fri, 20 Jan 2012 12:59:53 -0800
parents 72c96de7a403
children c4fa5e0b6193
comparison
equal deleted inserted replaced
14236:35903f035390 14237:11949c9795a0
207 error ("interpn: unrecognized interpolation METHOD"); 207 error ("interpn: unrecognized interpolation METHOD");
208 endif 208 endif
209 209
210 endfunction 210 endfunction
211 211
212
212 %!demo 213 %!demo
213 %! A=[13,-1,12;5,4,3;1,6,2]; 214 %! clf;
214 %! x=[0,1,4]; y=[10,11,12]; 215 %! A = [13,-1,12;5,4,3;1,6,2];
215 %! xi=linspace(min(x),max(x),17); 216 %! x = [0,1,4]; y = [10,11,12];
216 %! yi=linspace(min(y),max(y),26)'; 217 %! xi = linspace (min(x), max(x), 17);
217 %! mesh(xi,yi,interpn(x,y,A.',xi,yi,"linear").'); 218 %! yi = linspace (min(y), max(y), 26)';
218 %! [x,y] = meshgrid(x,y); 219 %! mesh (xi, yi, interpn (x,y,A.',xi,yi, "linear").');
219 %! hold on; plot3(x(:),y(:),A(:),"b*"); hold off; 220 %! [x,y] = meshgrid (x,y);
221 %! hold on; plot3 (x(:),y(:),A(:),"b*"); hold off;
220 222
221 %!demo 223 %!demo
222 %! A=[13,-1,12;5,4,3;1,6,2]; 224 %! clf;
223 %! x=[0,1,4]; y=[10,11,12]; 225 %! A = [13,-1,12;5,4,3;1,6,2];
224 %! xi=linspace(min(x),max(x),17); 226 %! x = [0,1,4]; y = [10,11,12];
225 %! yi=linspace(min(y),max(y),26)'; 227 %! xi = linspace (min(x), max(x), 17);
226 %! mesh(xi,yi,interpn(x,y,A.',xi,yi,"nearest").'); 228 %! yi = linspace (min(y), max(y), 26)';
227 %! [x,y] = meshgrid(x,y); 229 %! mesh (xi, yi, interpn (x,y,A.',xi,yi, "nearest").');
228 %! hold on; plot3(x(:),y(:),A(:),"b*"); hold off; 230 %! [x,y] = meshgrid (x,y);
229 231 %! hold on; plot3 (x(:),y(:),A(:),"b*"); hold off;
230 %!#demo 232
231 %! A=[13,-1,12;5,4,3;1,6,2]; 233 %!#demo # FIXME: Uncomment when support for "cubic" has been added
232 %! x=[0,1,2]; y=[10,11,12]; 234 %! clf;
233 %! xi=linspace(min(x),max(x),17); 235 %! A = [13,-1,12;5,4,3;1,6,2];
234 %! yi=linspace(min(y),max(y),26)'; 236 %! x = [0,1,2]; y = [10,11,12];
235 %! mesh(xi,yi,interpn(x,y,A.',xi,yi,"cubic").'); 237 %! xi = linspace (min(x), max(x), 17);
236 %! [x,y] = meshgrid(x,y); 238 %! yi = linspace (min(y), max(y), 26)';
237 %! hold on; plot3(x(:),y(:),A(:),"b*"); hold off; 239 %! mesh (xi, yi, interpn (x,y,A.',xi,yi, "cubic").');
240 %! [x,y] = meshgrid (x,y);
241 %! hold on; plot3 (x(:),y(:),A(:),"b*"); hold off;
238 242
239 %!demo 243 %!demo
240 %! A=[13,-1,12;5,4,3;1,6,2]; 244 %! clf;
241 %! x=[0,1,2]; y=[10,11,12]; 245 %! A = [13,-1,12;5,4,3;1,6,2];
242 %! xi=linspace(min(x),max(x),17); 246 %! x = [0,1,2]; y = [10,11,12];
243 %! yi=linspace(min(y),max(y),26)'; 247 %! xi = linspace (min(x), max(x), 17);
244 %! mesh(xi,yi,interpn(x,y,A.',xi,yi,"spline").'); 248 %! yi = linspace (min(y), max(y), 26)';
245 %! [x,y] = meshgrid(x,y); 249 %! mesh (xi, yi, interpn (x,y,A.',xi,yi, "spline").');
246 %! hold on; plot3(x(:),y(:),A(:),"b*"); hold off; 250 %! [x,y] = meshgrid (x,y);
247 251 %! hold on; plot3 (x(:),y(:),A(:),"b*"); hold off;
248 252
249 %!demo 253 %!demo
254 %! clf;
250 %! x = y = z = -1:1; 255 %! x = y = z = -1:1;
251 %! f = @(x,y,z) x.^2 - y - z.^2; 256 %! f = @(x,y,z) x.^2 - y - z.^2;
252 %! [xx, yy, zz] = meshgrid (x, y, z); 257 %! [xx, yy, zz] = meshgrid (x, y, z);
253 %! v = f (xx,yy,zz); 258 %! v = f (xx,yy,zz);
254 %! xi = yi = zi = -1:0.1:1; 259 %! xi = yi = zi = -1:0.1:1;
255 %! [xxi, yyi, zzi] = ndgrid (xi, yi, zi); 260 %! [xxi, yyi, zzi] = ndgrid (xi, yi, zi);
256 %! vi = interpn(x, y, z, v, xxi, yyi, zzi, 'spline'); 261 %! vi = interpn (x, y, z, v, xxi, yyi, zzi, 'spline');
257 %! mesh (yi, zi, squeeze (vi(1,:,:))); 262 %! mesh (yi, zi, squeeze (vi(1,:,:)));
258 263
259 264 %!test
260 %!test 265 %! [x,y,z] = ndgrid (0:2);
261 %! [x,y,z] = ndgrid(0:2); 266 %! f = x + y + z;
262 %! f = x+y+z; 267 %! assert (interpn (x,y,z,f,[.5 1.5],[.5 1.5],[.5 1.5]), [1.5, 4.5]);
263 %! assert (interpn(x,y,z,f,[.5 1.5],[.5 1.5],[.5 1.5]), [1.5, 4.5]) 268 %! assert (interpn (x,y,z,f,[.51 1.51],[.51 1.51],[.51 1.51],"nearest"), [3, 6]);
264 %! assert (interpn(x,y,z,f,[.51 1.51],[.51 1.51],[.51 1.51],'nearest'), [3, 6]) 269 %! assert (interpn (x,y,z,f,[.5 1.5],[.5 1.5],[.5 1.5],"spline"), [1.5, 4.5]);
265 %! assert (interpn(x,y,z,f,[.5 1.5],[.5 1.5],[.5 1.5],'spline'), [1.5, 4.5]) 270 %! assert (interpn (x,y,z,f,x,y,z), f);
266 %! assert (interpn(x,y,z,f,x,y,z), f) 271 %! assert (interpn (x,y,z,f,x,y,z,"nearest"), f);
267 %! assert (interpn(x,y,z,f,x,y,z,'nearest'), f) 272 %! assert (interpn (x,y,z,f,x,y,z,"spline"), f);
268 %! assert (interpn(x,y,z,f,x,y,z,'spline'), f)
269 273
270 %!test 274 %!test
271 %! [x, y, z] = ndgrid (0:2, 1:4, 2:6); 275 %! [x, y, z] = ndgrid (0:2, 1:4, 2:6);
272 %! f = x + y + z; 276 %! f = x + y + z;
273 %! xi = [0.5 1.0 1.5]; 277 %! xi = [0.5 1.0 1.5]; yi = [1.5 2.0 2.5 3.5]; zi = [2.5 3.5 4.0 5.0 5.5];
274 %! yi = [1.5 2.0 2.5 3.5];
275 %! zi = [2.5 3.5 4.0 5.0 5.5];
276 %! fi = interpn (x, y, z, f, xi, yi, zi); 278 %! fi = interpn (x, y, z, f, xi, yi, zi);
277 %! [xi, yi, zi] = ndgrid (xi, yi, zi); 279 %! [xi, yi, zi] = ndgrid (xi, yi, zi);
278 %! assert (fi, xi + yi + zi) 280 %! assert (fi, xi + yi + zi);
279 281
280 %!test 282 %!test
281 %! xi = 0:2; 283 %! xi = 0:2; yi = 1:4; zi = 2:6;
282 %! yi = 1:4;
283 %! zi = 2:6;
284 %! [x, y, z] = ndgrid (xi, yi, zi); 284 %! [x, y, z] = ndgrid (xi, yi, zi);
285 %! f = x + y + z; 285 %! f = x + y + z;
286 %! fi = interpn (x, y, z, f, xi, yi, zi, "nearest"); 286 %! fi = interpn (x, y, z, f, xi, yi, zi, "nearest");
287 %! assert (fi, x + y + z) 287 %! assert (fi, x + y + z);
288 288
289 %!test 289 %!test
290 %! [x,y,z] = ndgrid(0:2); 290 %! [x,y,z] = ndgrid (0:2);
291 %! f = x.^2+y.^2+z.^2; 291 %! f = x.^2 + y.^2 + z.^2;
292 %! assert (interpn(x,y,-z,f,1.5,1.5,-1.5), 7.5) 292 %! assert (interpn (x,y,-z,f,1.5,1.5,-1.5), 7.5);
293 293
294 %!test % for Matlab-compatible rounding for 'nearest' 294 %!test # for Matlab-compatible rounding for "nearest"
295 %! X = meshgrid (1:4); 295 %! x = meshgrid (1:4);
296 %! assert (interpn (X, 2.5, 2.5, 'nearest'), 3); 296 %! assert (interpn (x, 2.5, 2.5, "nearest"), 3);
297 297
298 %!shared z, zout, tol 298 %!test
299 %! z = zeros (3, 3, 3); 299 %! z = zeros (3, 3, 3);
300 %! zout = zeros (5, 5, 5); 300 %! zout = zeros (5, 5, 5);
301 %! z(:,:,1) = [1 3 5; 3 5 7; 5 7 9]; 301 %! z(:,:,1) = [1 3 5; 3 5 7; 5 7 9];
302 %! z(:,:,2) = z(:,:,1) + 2; 302 %! z(:,:,2) = z(:,:,1) + 2;
303 %! z(:,:,3) = z(:,:,2) + 2; 303 %! z(:,:,3) = z(:,:,2) + 2;
304 %! for n = 1:5 304 %! for n = 1:5
305 %! zout(:,:,n) = [1 2 3 4 5; 305 %! zout(:,:,n) = [1 2 3 4 5;
306 %! 2 3 4 5 6; 306 %! 2 3 4 5 6;
307 %! 3 4 5 6 7; 307 %! 3 4 5 6 7;
308 %! 4 5 6 7 8; 308 %! 4 5 6 7 8;
309 %! 5 6 7 8 9] + (n-1); 309 %! 5 6 7 8 9] + (n-1);
310 %! end 310 %! endfor
311 %! tol = 10 * eps; 311 %! tol = 10*eps;
312 %!assert (interpn (z), zout, tol) 312 %! assert (interpn (z), zout, tol);
313 %!assert (interpn (z, "linear"), zout, tol) 313 %! assert (interpn (z, "linear"), zout, tol);
314 %!assert (interpn (z, "spline"), zout, tol) 314 %! assert (interpn (z, "spline"), zout, tol);
315