<html lang="en"> <head> <title>Multi-dimensional Interpolation - Untitled</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="Untitled"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="Interpolation.html#Interpolation" title="Interpolation"> <link rel="prev" href="One_002ddimensional-Interpolation.html#One_002ddimensional-Interpolation" title="One-dimensional Interpolation"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="Multi-dimensional-Interpolation"></a> <a name="Multi_002ddimensional-Interpolation"></a> <p> Previous: <a rel="previous" accesskey="p" href="One_002ddimensional-Interpolation.html#One_002ddimensional-Interpolation">One-dimensional Interpolation</a>, Up: <a rel="up" accesskey="u" href="Interpolation.html#Interpolation">Interpolation</a> <hr> </div> <h3 class="section">28.2 Multi-dimensional Interpolation</h3> <p>There are three multi-dimensional interpolation functions in Octave, with similar capabilities. Methods using Delaunay tessellation are described in <a href="Interpolation-on-Scattered-Data.html#Interpolation-on-Scattered-Data">Interpolation on Scattered Data</a>. <!-- ./general/interp2.m --> <p><a name="doc_002dinterp2"></a> <div class="defun"> — Function File: <var>zi</var> = <b>interp2</b> (<var>x, y, z, xi, yi</var>)<var><a name="index-interp2-2066"></a></var><br> — Function File: <var>zi</var> = <b>interp2</b> (<var>Z, xi, yi</var>)<var><a name="index-interp2-2067"></a></var><br> — Function File: <var>zi</var> = <b>interp2</b> (<var>Z, n</var>)<var><a name="index-interp2-2068"></a></var><br> — Function File: <var>zi</var> = <b>interp2</b> (<var><small class="dots">...</small>, method</var>)<var><a name="index-interp2-2069"></a></var><br> — Function File: <var>zi</var> = <b>interp2</b> (<var><small class="dots">...</small>, method, extrapval</var>)<var><a name="index-interp2-2070"></a></var><br> <blockquote> <p>Two-dimensional interpolation. <var>x</var>, <var>y</var> and <var>z</var> describe a surface function. If <var>x</var> and <var>y</var> are vectors their length must correspondent to the size of <var>z</var>. <var>x</var> and <var>y</var> must be monotonic. If they are matrices they must have the <code>meshgrid</code> format. <dl> <dt><code>interp2 (</code><var>x</var><code>, </code><var>y</var><code>, </code><var>Z</var><code>, </code><var>xi</var><code>, </code><var>yi</var><code>, ...)</code><dd>Returns a matrix corresponding to the points described by the matrices <var>xi</var>, <var>yi</var>. <p>If the last argument is a string, the interpolation method can be specified. The method can be 'linear', 'nearest' or 'cubic'. If it is omitted 'linear' interpolation is assumed. <br><dt><code>interp2 (</code><var>z</var><code>, </code><var>xi</var><code>, </code><var>yi</var><code>)</code><dd>Assumes <var>x</var><code> = 1:rows (</code><var>z</var><code>)</code> and <var>y</var><code> = 1:columns (</code><var>z</var><code>)</code> <br><dt><code>interp2 (</code><var>z</var><code>, </code><var>n</var><code>)</code><dd>Interleaves the matrix <var>z</var> n-times. If <var>n</var> is omitted a value of <var>n</var><code> = 1</code> is assumed. </dl> <p>The variable <var>method</var> defines the method to use for the interpolation. It can take one of the following values <dl> <dt>'nearest'<dd>Return the nearest neighbor. <br><dt>'linear'<dd>Linear interpolation from nearest neighbors. <br><dt>'pchip'<dd>Piece-wise cubic hermite interpolating polynomial (not implemented yet). <br><dt>'cubic'<dd>Cubic interpolation from four nearest neighbors. <br><dt>'spline'<dd>Cubic spline interpolation–smooth first and second derivatives throughout the curve. </dl> <p>If a scalar value <var>extrapval</var> is defined as the final value, then values outside the mesh as set to this value. Note that in this case <var>method</var> must be defined as well. If <var>extrapval</var> is not defined then NA is assumed. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dinterp1.html#doc_002dinterp1">interp1</a>. </p></blockquote></div> <!-- ./general/interp3.m --> <p><a name="doc_002dinterp3"></a> <div class="defun"> — Function File: <var>vi</var> = <b>interp3</b> (<var>x, y,z, v, xi, yi, zi</var>)<var><a name="index-interp3-2071"></a></var><br> — Function File: <var>vi</var> = <b>interp3</b> (<var>v, xi, yi, zi</var>)<var><a name="index-interp3-2072"></a></var><br> — Function File: <var>vi</var> = <b>interp3</b> (<var>v, m</var>)<var><a name="index-interp3-2073"></a></var><br> — Function File: <var>vi</var> = <b>interp3</b> (<var>v</var>)<var><a name="index-interp3-2074"></a></var><br> — Function File: <var>vi</var> = <b>interp3</b> (<var><small class="dots">...</small>, method</var>)<var><a name="index-interp3-2075"></a></var><br> — Function File: <var>vi</var> = <b>interp3</b> (<var><small class="dots">...</small>, method, extrapval</var>)<var><a name="index-interp3-2076"></a></var><br> <blockquote> <p>Perform 3-dimensional interpolation. Each element of the 3-dimensional array <var>v</var> represents a value at a location given by the parameters <var>x</var>, <var>y</var>, and <var>z</var>. The parameters <var>x</var>, <var>x</var>, and <var>z</var> are either 3-dimensional arrays of the same size as the array <var>v</var> in the 'meshgrid' format or vectors. The parameters <var>xi</var>, etc. respect a similar format to <var>x</var>, etc., and they represent the points at which the array <var>vi</var> is interpolated. <p>If <var>x</var>, <var>y</var>, <var>z</var> are omitted, they are assumed to be <code>x = 1 : size (</code><var>v</var><code>, 2)</code>, <code>y = 1 : size (</code><var>v</var><code>, 1)</code> and <code>z = 1 : size (</code><var>v</var><code>, 3)</code>. If <var>m</var> is specified, then the interpolation adds a point half way between each of the interpolation points. This process is performed <var>m</var> times. If only <var>v</var> is specified, then <var>m</var> is assumed to be <code>1</code>. <p>Method is one of: <dl> <dt>'nearest'<dd>Return the nearest neighbor. <br><dt>'linear'<dd>Linear interpolation from nearest neighbors. <br><dt>'cubic'<dd>Cubic interpolation from four nearest neighbors (not implemented yet). <br><dt>'spline'<dd>Cubic spline interpolation–smooth first and second derivatives throughout the curve. </dl> <p>The default method is 'linear'. <p>If <var>extrap</var> is the string 'extrap', then extrapolate values beyond the endpoints. If <var>extrap</var> is a number, replace values beyond the endpoints with that number. If <var>extrap</var> is missing, assume NA. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dinterp1.html#doc_002dinterp1">interp1</a>, <a href="doc_002dinterp2.html#doc_002dinterp2">interp2</a>, <a href="doc_002dspline.html#doc_002dspline">spline</a>, <a href="doc_002dmeshgrid.html#doc_002dmeshgrid">meshgrid</a>. </p></blockquote></div> <!-- ./general/interpn.m --> <p><a name="doc_002dinterpn"></a> <div class="defun"> — Function File: <var>vi</var> = <b>interpn</b> (<var>x1, x2, <small class="dots">...</small>, v, y1, y2, <small class="dots">...</small></var>)<var><a name="index-interpn-2077"></a></var><br> — Function File: <var>vi</var> = <b>interpn</b> (<var>v, y1, y2, <small class="dots">...</small></var>)<var><a name="index-interpn-2078"></a></var><br> — Function File: <var>vi</var> = <b>interpn</b> (<var>v, m</var>)<var><a name="index-interpn-2079"></a></var><br> — Function File: <var>vi</var> = <b>interpn</b> (<var>v</var>)<var><a name="index-interpn-2080"></a></var><br> — Function File: <var>vi</var> = <b>interpn</b> (<var><small class="dots">...</small>, method</var>)<var><a name="index-interpn-2081"></a></var><br> — Function File: <var>vi</var> = <b>interpn</b> (<var><small class="dots">...</small>, method, extrapval</var>)<var><a name="index-interpn-2082"></a></var><br> <blockquote> <p>Perform <var>n</var>-dimensional interpolation, where <var>n</var> is at least two. Each element of the <var>n</var>-dimensional array <var>v</var> represents a value at a location given by the parameters <var>x1</var>, <var>x2</var>, <small class="dots">...</small>, <var>xn</var>. The parameters <var>x1</var>, <var>x2</var>, <small class="dots">...</small>, <var>xn</var> are either <var>n</var>-dimensional arrays of the same size as the array <var>v</var> in the 'ndgrid' format or vectors. The parameters <var>y1</var>, etc. respect a similar format to <var>x1</var>, etc., and they represent the points at which the array <var>vi</var> is interpolated. <p>If <var>x1</var>, <small class="dots">...</small>, <var>xn</var> are omitted, they are assumed to be <code>x1 = 1 : size (</code><var>v</var><code>, 1)</code>, etc. If <var>m</var> is specified, then the interpolation adds a point half way between each of the interpolation points. This process is performed <var>m</var> times. If only <var>v</var> is specified, then <var>m</var> is assumed to be <code>1</code>. <p>Method is one of: <dl> <dt>'nearest'<dd>Return the nearest neighbor. <br><dt>'linear'<dd>Linear interpolation from nearest neighbors. <br><dt>'cubic'<dd>Cubic interpolation from four nearest neighbors (not implemented yet). <br><dt>'spline'<dd>Cubic spline interpolation–smooth first and second derivatives throughout the curve. </dl> <p>The default method is 'linear'. <p>If <var>extrapval</var> is the scalar value, use it to replace the values beyond the endpoints with that number. If <var>extrapval</var> is missing, assume NA. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dinterp1.html#doc_002dinterp1">interp1</a>, <a href="doc_002dinterp2.html#doc_002dinterp2">interp2</a>, <a href="doc_002dspline.html#doc_002dspline">spline</a>, <a href="doc_002dndgrid.html#doc_002dndgrid">ndgrid</a>. </p></blockquote></div> <p>A significant difference between <code>interpn</code> and the other two multidimensional interpolation functions is the fashion in which the dimensions are treated. For <code>interp2</code> and <code>interp3</code>, the 'y' axis is considered to be the columns of the matrix, whereas the 'x' axis corresponds to the rows of the array. As Octave indexes arrays in column major order, the first dimension of any array is the columns, and so <code>interpn</code> effectively reverses the 'x' and 'y' dimensions. Consider the example <pre class="example"> x = y = z = -1:1; f = @(x,y,z) x.^2 - y - z.^2; [xx, yy, zz] = meshgrid (x, y, z); v = f (xx,yy,zz); xi = yi = zi = -1:0.1:1; [xxi, yyi, zzi] = meshgrid (xi, yi, zi); vi = interp3(x, y, z, v, xxi, yyi, zzi, 'spline'); [xxi, yyi, zzi] = ndgrid (xi, yi, zi); vi2 = interpn(x, y, z, v, xxi, yyi, zzi, 'spline'); mesh (zi, yi, squeeze (vi2(1,:,:))); </pre> <p class="noindent">where <code>vi</code> and <code>vi2</code> are identical. The reversal of the dimensions is treated in the <code>meshgrid</code> and <code>ndgrid</code> functions respectively. <p>In additional the support function <code>bicubic</code> that underlies the cubic interpolation of <code>interp2</code> function can be called directly. <!-- ./general/bicubic.m --> <p><a name="doc_002dbicubic"></a> <div class="defun"> — Function File: <var>zi</var> = <b>bicubic</b> (<var>x, y, z, xi, yi, extrapval</var>)<var><a name="index-bicubic-2083"></a></var><br> <blockquote> <p>Return a matrix <var>zi</var> corresponding to the bicubic interpolations at <var>xi</var> and <var>yi</var> of the data supplied as <var>x</var>, <var>y</var> and <var>z</var>. Points outside the grid are set to <var>extrapval</var>. <p>See <a href="http://wiki.woodpecker.org.cn/moin/Octave/Bicubic">http://wiki.woodpecker.org.cn/moin/Octave/Bicubic</a> for further information. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dinterp2.html#doc_002dinterp2">interp2</a>. </p></blockquote></div> <!-- DO NOT EDIT! Generated automatically by munge-texi. --> <!-- Copyright (C) 2007, 2008, 2009 John W. Eaton and David Bateman --> <!-- This file is part of Octave. --> <!-- Octave is free software; you can redistribute it and/or modify it --> <!-- under the terms of the GNU General Public License as published by the --> <!-- Free Software Foundation; either version 3 of the License, or (at --> <!-- your option) any later version. --> <!-- Octave is distributed in the hope that it will be useful, but WITHOUT --> <!-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or --> <!-- FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --> <!-- for more details. --> <!-- You should have received a copy of the GNU General Public License --> <!-- along with Octave; see the file COPYING. If not, see --> <!-- <http://www.gnu.org/licenses/>. --> </body></html>