如何从numpy矩阵中提取任意一行值?

时间:2016-09-12 09:20:23

标签: python numpy scipy

我想从等距3D矩阵中提取两个任意点之间的插值直线,例如:

data_points_on_line=extract_line_from_3D_matrix(matrix,point1,point2,number_of_data_values_needed)

包含3D矩阵matrix,起点和终点point1point2以及point1point2之间返回数据的分辨率,由整数给出与number_of_data_values_needed

我知道使用scipy.ndimage从二维数组中提取数据非常简单,但有人知道scipy或{{1}中是否已有函数哪个可以用3D做到这一点?

提前多多感谢!

1 个答案:

答案 0 :(得分:1)

它适用于我使用InterpolatingFunction,例如:

#!/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