indices = tuple([np.arange(j) for j in b.shape[:-1]])
indices = np.meshgrid(indexing='ij', sparse=True, *indices)
indices.append(index)
indices = tuple(indices)
Python/Scipy: Find "bounded" min/max of a matrix