方法1
这是一个
broadcasting-
def takeN_le_per_row_broadcasting(a, b, N=3): # a, b : 1D, 2D arrays respectively # First col indices in each row of b with <= corresponding one in a idx = (b <= a[:,None]).argmax(1) # Get all N ranged column indices all_idx = idx[:,None] + np.arange(N) # Finally advanced-index with those indices into b for desired output return b[np.arange(len(all_idx))[:,None], all_idx]
方法#2
受
NumPy Fancy Indexing - Crop different ROIs from differentchannels的解决方案启发,我们可以利用
np.lib.stride_tricks.as_strided高效的补丁提取,例如-
from skimage.util.shape import view_as_windowsdef takeN_le_per_row_strides(a, b, N=3): # a, b : 1D, 2D arrays respectively # First col indices in each row of b with <= corresponding one in a idx = (b <= a[:,None]).argmax(1) # Get 1D sliding windows for each element off data w = view_as_windows(b, (1,N))[:,:,0] # Use fancy/advanced indexing to select the required ones return w[np.arange(len(idx)), idx]
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)