n个一维数组组成n+1维数组,value等于索引值

tech2025-07-05  1

以n=3为例

# 要生成的三维数组各向维度 Z, Y, X = 90, 250, 410 # 定义索引值 z = np.array(range(Z)) y = np.array(range(Y)) x = np.array(range(X)) # 各维度分别生成三维数组,value为该维度索引 z = z.reshape(-1,1).repeat(Y,axis=1) z = np.expand_dims(z,axis=2).repeat(X,axis=2) y = y.reshape(1,-1).repeat(Z,axis=0) y = np.expand_dims(y,axis=2).repeat(X,axis=2) x = x.reshape(1,-1).repeat(Y,axis=0) x = np.expand_dims(x,axis=0).repeat(Z,axis=0) # 拼接成所要数组 pos = np.zeros((Z,Y,X,3)) pos[:,:,:,0] = z pos[:,:,:,1] = y pos[:,:,:,2] = x

输出

pos[45,15,206] Out[0]: array([ 45., 15., 206.]) pos[45,15,26] Out[1]: array([45., 15., 26.])

 

最新回复(0)