简体   繁体   English

在numpy 2D矩阵中计算“孔”

[英]Counting “wells” in a numpy 2D matrix

Given a 2D matrix of 1s and 0s, for eg - 给定1s和0s的2D矩阵,例如-

array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 1, 1, 0, 0],
       [0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 1, 1, 0, 0],
       [0, 0, 0, 0, 0, 0, 1, 1, 0, 0],
       [0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 1, 1, 0, 0],
       [0, 0, 0, 0, 0, 0, 1, 1, 1, 1],
       [0, 1, 1, 0, 0, 1, 0, 1, 0, 1],
       [1, 1, 0, 0, 1, 1, 0, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 0, 1, 0, 0],
       [1, 0, 0, 0, 1, 0, 1, 1, 0, 0],
       [1, 0, 0, 0, 1, 0, 1, 1, 1, 0],
       [1, 0, 0, 0, 1, 1, 0, 1, 1, 0]])

1 indicates a block, and 0 indicates empty space. 1表示块,0表示空白。 I wish to calculate the number of wells , and their accumulated depth . 我想计算一下数量及其累积深度

A well exists when a column is shorter than both its adjacent columns, borders are assumed to be filled with blocks (1s). 当一列短于其相邻两列时,就存在一口井,假定边界被块(1s)填充。 For eg, padding the borders, the array becomes: 例如,填充边框,数组变为:

array([[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
       [1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1],
       [1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1],
       [1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1],
       [1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1],
       [1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1],
       [1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1],
       [1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1],
       [1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1],
       [1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1],
       [1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1],
       [1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1],
       [1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1],
       [1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1]])
  • The number of wells is 3 ( Column 1, 4, and (9,10) ). 孔数为3第1列,第4列和(9,10) )。

  • The depth of a well is min(height(col_to_left), height(col_to_right)) - height(well_col) . 孔的深度是min(height(col_to_left), height(col_to_right)) - height(well_col)
    So, in this case, the depths are [1, 1, 7] . 因此,在这种情况下,深度为[ 1,1,7 ] And therefore the accumulated depth is 1+1+7= 9 . 因此,累积深度为1 + 1 + 7 = 9

How would I go about finding this? 我将如何找到这个? I want to avoid using loops. 我想避免使用循环。

You can use argmax to get the positions of the first one in each column. 您可以使用argmax获取每列中第一个的位置。

# find absolute depths
d = X.argmax(0)
# correct columns that are all zero
v = np.take_along_axis(X, d[None], 0)
d[v[0]==0] = len(X)
# pad and compute the col to next col changes
d = np.diff(np.concatenate([[0], d, [0]]))
# exclude no change
chng, = np.where(d)
# find down changes
dwn = d[chng]>0
# wells are where we go down and the next non zero change is up
well = dwn[:-1]&~dwn[1:]
# map back to unfiltered indices for left and right walls
l, r = chng[:-1][well], chng[1:][well]
wells = np.c_[l+1, r]
wells
# array([[ 1,  1],
#       [ 4,  4],
#       [ 9, 10]])
# retrieve relative depths
depths = np.minimum(d[l], -d[r])
depths
# array([1, 1, 7])

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

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