简体   繁体   English

如何在2D数组中查找值的地址

[英]How to find the address of a value in an 2D array

I found a max_value (grayscale 2D array [50,200]) with 我发现了一个max_value(灰度2D数组[50,200])

max = numpy.amax(matrice)

so it gives me a value. 所以它给了我一个价值。 Now I want to know were I can find this value. 现在我想知道是否可以找到该值。 I tried this code: 我尝试了这段代码:

for x in range (1,50):  
for y in range (1,200):
if matrice[x,y]==max:
print ("[%s,%s]",x,y)

but it gives me an error message: 但是它给我一个错误信息:

"for y in range (1,200): IndentationError: expected an indented block" “对于y,范围为(1200):IndentationError:应为缩进块”

Can someone tell me what is the error or how I can find the address of this pixel? 有人可以告诉我这是什么错误或如何找到该像素的地址吗?

The error is because you did not indent the line after the colon, so the syntax should look something like this: 错误是因为您没有在冒号后缩进行,所以语法应如下所示:

for x in range (1,50):  
    for y in range (1,200):
        if matrice[x,y]==max:
            print ("[%s,%s]",x,y)

Try looking up the rules for indentation in Python, if you are in doubt about when you should use indentation. 如果您不确定何时应该使用缩进,请尝试在Python中查找缩进规则。

You have to give tab indentation for each block 您必须为每个块提供制表符缩进

for x in range (1,50):  
    for y in range (1,200):
        if matrice[x,y]==max:
            print ("[%s,%s]",x,y)

As an alternative, you can vectorise your logic. 或者,您可以向量化逻辑。

A = np.random.randint(0, 10, (5, 5))

print(A)

# [[4 9 3 0 8]
#  [5 0 3 9 0]
#  [2 2 4 1 5]
#  [3 5 9 0 0]
#  [9 5 7 9 5]]

res = np.argwhere(A==np.amax(A))

print(res)

# [[0 1]
#  [1 3]
#  [3 2]
#  [4 0]
#  [4 3]]

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

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