简体   繁体   English

numpy-数组的复杂修改

[英]Numpy -complex modification of arrays

Suppose I have a int16 numpy array such as: 假设我有一个int16 numpy数组,例如:

 ([[ 1, 1, 1, 1, 1, 1, 1],

   [ 1, 0, 1, 1, 1, 1, 1],

   [ 0, 0, 0, 1, 1, 1, 0],

   [ 1, 0, 0, 0, 0, 1, 0],

   [ 0, 1, 0, 0, 0, 1, 0]])

I'd like to modify this array so that every 1 with a 0 in the space directly below it becomes a 21. I'd also like to do this on multiple, much larger arrays. 我想修改此数组,以便在其正下方的空间中每个带有1的1都变成21。我也想对多个更大的数组进行此操作。 What is the fastest way to carry this out? 最快的方法是什么?

Assuming the last row is supposed to stay the same you can to the following. 假设最后一行应该保持不变,您可以执行以下操作。

sel = (A[:-1] == 1) * (A[1:] == 0)
A[:-1][sel] = 21

Here A is your matrix. 这里A是你的矩阵。 The first line creates a masked array of size (m - 1, n) with m, n = A.shape . 第一行创建一个掩码数组,大小为(m - 1, n)m, n = A.shape

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM