1

I need a function that finds the biggest value and prints its coordinates. In case of more than one maximum element, that all of them should be printed.

Example of distance matrix:

[[0.         0.4        0.66666667 0.85714286]
 [0.4        0.         0.4        0.85714286]
 [0.66666667 0.4        0.         1.        ]
 [0.85714286 0.85714286 1.         0.        ]]

My function, which gives only the last largest distance:

def maxelement(arr):
    cords = []
    no_of_rows = len(arr) 
    no_of_column = len(arr[0])   
    for i in range(no_of_rows): 
        max1 = 0
        for j in range(no_of_column): 
            if arr[i][j] > max1 : 
                max1 = arr[i][j]
                o = i
                a = j
    cords.append(a+1)
    cords.append(o+1)
    print("The biggest distance is:", max1,", for sets:", cords)
Inde7
  • 87
  • 7
  • Does this answer your question? [How to get the index of a maximum element in a numpy array along one axis](https://stackoverflow.com/questions/5469286/how-to-get-the-index-of-a-maximum-element-in-a-numpy-array-along-one-axis) – snwflk Jun 18 '20 at 15:12

2 Answers2

0

This will work. Let me know if you have any questions.

def maxelement(arr):
    cords = []
    no_of_rows = len(arr) 
    no_of_column = len(arr[0])
    coords = [(R,C) for R in range(no_of_rows) for C in range(no_of_column)] # W and H are nicer here...
    max1 = max([arr[R][C] for R,C in coords])
    cords = [(R,C) for R,C in coords if arr[R][C] == max1]
    print("The biggest distance is:", max1,", for sets:", cords)



A = [[1,2,3],[4,5,6],[7,8,9]]
maxelement(A)
JimmyCarlos
  • 1,934
  • 1
  • 10
  • 24
0
def maxelement(arr):
    cords = []
    no_of_rows = len(arr) 
    no_of_column = len(arr[0])
    max1 = max(max(arr))  
    for i in range(no_of_rows):
        for j in range(no_of_rows):
            if arr[i][j] == max1:
                cords.append([i,j])
    print("The biggest distance is:", max1,", for sets:", cords)
array = [[0.0,0.4,0.66666667,0.85714286],[0.4,0.0,0.4,0.85714286],[0.66666667,0.4,0.0,1.0],[0.85714286,0.85714286, 1.,0.,]]
maxelement(array)
Anto Pravin
  • 378
  • 2
  • 9