How to extract an arbitrary line of values from a numpy matrix?
<p>i want to extract an interpolated straight line between two arbitrary points from a equidistant 3D matrix, such as:</p>

<pre><code>data_points_on_line=extract_line_from_3D_matrix(matrix,point1,point2,number_of_data_values_needed)
</code></pre>

<p>with the 3D matrix <code>matrix</code>, start and end point <code>point1</code>,<code>point2</code> and the resolution of returned data between <code>point1</code> and <code>point2</code> given by an integer with <code>number_of_data_values_needed</code>.</p>

<p>i know that it's pretty simple when extracting data from a 2d array with <code>scipy.ndimage</code> but does anybody know whether there's already a function in <code>scipy</code> or <code>numpy</code> which can do that in 3D?</p>

<p>many thanks in advance!</p>

<p>it works for me using InterpolatingFunction, for example:</p>

<pre><code>#!/usr/bin/python
import numpy as np

from Scientific.Functions.Interpolation import InterpolatingFunction

def linear_interp_3d(point1,point2,spacing):
    xi=np.linspace(point1[0],point2[0],spacing)
    yi=np.linspace(point1[1],point2[1],spacing)
    zi=np.linspace(point1[2],point2[2],spacing)

    return xi,yi,zi

n=4
x = np.arange(0, n, 1)
y = np.arange(0, n, 1)
z = np.arange(0, n, 1)

data = np.ones(shape = (n, n, n))
for dummy in range(n):
    data[dummy,:,:]=dummy**3
axes = (x,y,z)
f = InterpolatingFunction(axes, data)

point1=(0,0,0)
point2=(3,3,3)
linevals=linear_interp_3d(point1,point2,11)

line=[]
for dummy in range(len(linevals[0][:])):
    line.append(f(linevals[0][dummy],linevals[1][dummy],linevals[2][dummy]))
print line
</code></pre>

python numpy scipy