通用将数组元素的邻居堆叠到3-D数组中

通用将数组元素的邻居堆叠到3-D数组中,第1张

通用将数组元素的邻居堆叠到3-D数组中

这可能是一种方法-

import numpy as np# ParametersR = 3  # RadiusM1,N1 = padded.shaperowlen = N1 - R + 1collen = M1 - R + 1# Linear indices for the starting R x R blockidx1 = np.arange(R)[:,None]*N1 + np.arange(R)# Offset (from the starting block indices) linear indices for all the blocksidx2 = np.arange(collen)[:,None]*N1 + np.arange(rowlen)# Finally, get the linear indices for all blocksall_idx = idx1.ravel()[None,None,:] + idx2[:,:,None]# Index into padded for the final outputout = padded.ravel()[all_idx]

这是半径的示例运行

R = 4
-

In [259]: paddedOut[259]: array([[ 5,  5,  0,  3,  3,  3],       [ 5,  5,  0,  3,  3,  3],       [ 7,  7,  9,  3,  5,  5],       [ 2,  2,  4,  7,  6,  6],       [ 8,  8,  8, 10,  1,  1],       [ 6,  6,  7,  7,  8,  8],       [ 6,  6,  7,  7,  8,  8]])In [260]: outOut[260]: array([[[ 5,  5,  0,  3,  5,  5,  0,  3,  7,  7,  9,  3,  2,  2,  4,  7],        [ 5,  0,  3,  3,  5,  0,  3,  3,  7,  9,  3,  5,  2,  4,  7,  6],        [ 0,  3,  3,  3,  0,  3,  3,  3,  9,  3,  5,  5,  4,  7,  6,  6]],       [[ 5,  5,  0,  3,  7,  7,  9,  3,  2,  2,  4,  7,  8,  8,  8, 10],        [ 5,  0,  3,  3,  7,  9,  3,  5,  2,  4,  7,  6,  8,  8, 10,  1],        [ 0,  3,  3,  3,  9,  3,  5,  5,  4,  7,  6,  6,  8, 10,  1,  1]],       [[ 7,  7,  9,  3,  2,  2,  4,  7,  8,  8,  8, 10,  6,  6,  7,  7],        [ 7,  9,  3,  5,  2,  4,  7,  6,  8,  8, 10,  1,  6,  7,  7,  8],        [ 9,  3,  5,  5,  4,  7,  6,  6,  8, 10,  1,  1,  7,  7,  8,  8]],       [[ 2,  2,  4,  7,  8,  8,  8, 10,  6,  6,  7,  7,  6,  6,  7,  7],        [ 2,  4,  7,  6,  8,  8, 10,  1,  6,  7,  7,  8,  6,  7,  7,  8],        [ 4,  7,  6,  6,  8, 10,  1,  1,  7,  7,  8,  8,  7,  7,  8,  8]]])


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5617545.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-15
下一篇2022-12-15

发表评论

登录后才能评论

评论列表(0条)

    保存