Home > Software design >  Number of column of the maximum element of a row in a matrix
Number of column of the maximum element of a row in a matrix

Time:02-08

I want to see the position of the maximum element in each row.

Input:
arr = [[3, 4, 1, 8],
       [1, 4, 9, 11],
       [76, 34, 21, 1],
       [2, 1, 4, 5]]

Output:
[3, 3, 0, 3]

I have already implemented this function, but I don't know why it's not working. Please help me.

# Python program to find maximum
# element of each row in a matrix
 
# importing numpy
import numpy
 
# Function to get max element
def maxelement(arr):
    listmax = []
     
    # get number of rows and columns
    no_of_rows = len(arr)
    no_of_column = len(arr[0])
     
    for i in range(no_of_rows):
         
        # Initialize max1 to 0 at beginning
        # of finding max element of each row
        max1 = 0
        for j in range(no_of_column):
            if arr[i][j] > max1 :
                max1 = arr[i][j]
                
        # print maximum element of each row
        listmax.append(max1)
        
    return(listmax)

CodePudding user response:

As you seem to be using numpy, I would suggest to make use of argmax here, which makes the problem a oneliner

listmax = np.argmax(arr,axis=1).tolist()

CodePudding user response:

Instead of appending the maximum value (max1) you want to append the index of the maximum:

# Python program to find maximum
# element of each row in a matrix
 
# importing numpy
import numpy
 
# Function to get max element
def maxelement(arr):
    listmax = []
     
    # get number of rows and columns
    no_of_rows = len(arr)
    no_of_column = len(arr[0])
     
    for i in range(no_of_rows):
         
        # Initialize max1 to 0 at beginning
        # of finding max element of each row
        max1 = 0
        max_index = 0
        for j in range(no_of_column):
            if arr[i][j] > max1 :
                max1 = arr[i][j]
                max_index = j
                
        # print maximum index of each row
        listmax.append(max_index)
        
    return(listmax)

CodePudding user response:

You can keep things easier here by using max() and index()

arr = [[3, 4, 1, 8],
       [1, 4, 9, 11],
       [76, 34, 21, 1],
       [2, 1, 4, 5]]


# Function to get max element
def maxelement(arr):
    res = []
    for elem in arr:
        res.append(elem.index(max(elem)))
    return res


print(maxelement(arr))

or even better

def maxelement(arr):
    return [elem.index(max(elem)) for elem  in arr]

CodePudding user response:

arr = [[3, 4, 1, 8],
       [1, 4, 9, 11],
       [76, 34, 21, 1],
       [2, 1, 4, 5]]
def largest_in_row(arr):
    return [row.index(max(row)) for row in arr]


print(largest_in_row(arr))

CodePudding user response:

An alternative to using max to get the max item and then index to find its index is to use max on the range of indices with a key that looks up the item:

>>> arr = [[3, 4, 1, 8],
       [1, 4, 9, 11],
       [76, 34, 21, 1],
       [2, 1, 4, 5]]
>>> [max(range(len(row)), key=row.__getitem__) for row in arr]
[3, 3, 0, 3]
  •  Tags:  
  • Related