给定一个源数组
src = np.random.rand(320,240)
和索引数组
idx = np.indices(src.shape).reshape(2, -1)
np.random.shuffle(idx.T)
我们可以将src
中的线性索引i
映射到目标数组dst
中的二维索引idx[:,i]
dst = np.empty_like(src)
dst[tuple(idx)] = src.ravel()
这将在Python: Mapping between two arrays with an index array中讨论
但是,如果此映射不是1对1的,即src
中的多个条目映射到dst
中的同一条目,according to the docs则未指定哪些源条目将写入dst
:
For advanced assignments, there is in general no guarantee for the iteration order. This means that if an element is set more than once, it is not possible to predict the final result.
如果我们另外得到一个优先数组
p = np.random.rand(*src.shape)
我们如何使用p
来消除这种情况,即根据p
编写优先级最高的条目?你知道吗
这里有一个使用稀疏矩阵进行排序的方法(它有很大的开销,但比argsort的伸缩性更好,可能是因为它使用了一些类似基数排序的方法(?)。没有优先级的重复索引显式设置为-1。我们使目标数组的一个单元太大,多余的单元充当垃圾桶。你知道吗
运行示例:
相关问题 更多 >
编程相关推荐