What I'm trying to do is simply find and define an x and y coordinate for the highest number in the array.
For example, 50000 would output: x = 2, y = 2. Is there an easy way to accomplish this?
I created this code below:
data_array = [[0, 1, 2, 3, 50000],
[5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]]
highest_num = data_array[0][0]
x = 0
y = 0
# looping from 0 to len(data_array)-1
for i in range(len(data_array)):
# looping from 0 to len(data_array[i])-1
for j in range(len(data_array[i])):
# checking data_array[x][y] is less than data_array[i][j]
if data_array[x][y] < data_array[i][j]:
# updating x and y
x = i
y = j
highest_num = data_array[i][j]
# printing the values of highest_num, x and y
print("highest_num =", highest_num)
print("x =", x)
print("y =", y)
But I would get x = 0, y = 4. I wanted to reference the middle of the array which is 12 and make the output be x = 2, y = 2.
Can this be accomplished without numpy where? I want the points to track with the max wherever it is.
CodePudding user response:
Modify 2 lines in your program:
print("x =", x-2)
print("y =", y 2)
CodePudding user response:
if you could use numpy do the argmax like this then find the coordinate (row col) and coordinate switch to chartersian (reverse y) then both subtracte the half , (considering it is even in both dimentions.
idx = np.argmax(data_array)
m, n = len(data_array), len(data_array[0])
r, c = m - (idx // n) - 1 , idx % n
y , x = r - (m // 2), c - (n // 2)
if not just use the code your writetn to find maximum index.