How to find the highest value of a matrix?

Asked

Viewed 697 times

0

Well, I have a 3x4 matrix and I want to know how to develop a python code to get the highest value of this matrix.

My code made so far was this:

LINHAS = 3
COLUNAS = 4
LINHA_
MatrizM = []

for i in range(LINHAS):
    MatrizM.append([])
    for j in range(COLUNAS):
        n = int(input())
        MatrizM[i].append(n)

print('=== MATRIZ M ===')
for i in range(LINHAS):
    for j in range(COLUNAS):
        print(MatrizM[i][j], end='\t')
    print()

What I don’t know is how I can develop a code that shows me the greatest value of this matrix

1 answer

0


You can create an IF condition inside the repeated, it will look like this:

controle = 0 (0 ou algum valor da matriz)
for(...)
.
if n > controle:
controle = n
.
.

Let’s assume that the first value is 10, the IF will test if 10 is greater than 0, as it will be TRUE the control variable gets 10.Let’s assume that the next value is 9, when testing again the result will be FALSE because 9 is less than 10, then the value will not overwrite itself.

As the friend suggested in the comment, it can happen that your matrix has only negative values, to get around this, the control variable needs to receive some value from the table instead of 0. This way there will be no problems with positive and negative numbers.

  • 2

    And if all matrix values are negative?

  • You can do the following, instead of putting control = 0, put control receiving any matrix value, this way the if will work with any value.

  • 1

    Then it would be interesting to address this in the answer and make it complete :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.