-4
m = int(input("Number of matrices: "))
rows = int(input("Enter the Number of rows : "))
column = int(input("Enter the Number of Columns: "))

p = 0
for i in range(0,m):
    print("Enter the elements of Matrix:")
    matrix_i = [[tuple(map(float, input().split(" "))) for i in range(column)] for i in range(rows)]
    p = p + 1
    print("Matrix no: ", p)
    for n in matrix_i:
        print(n)
        
res = tuple(map(lambda i, j: max(i ,j) , matrix_i, matrix_i))

print(res)

Got the output as:

Number of matrices: 
3
Enter the Number of rows : 
2
Enter the Number of Columns: 
2
Enter the elements of Matrix:
6 7 8
6 8 2
1 2 3
9 0 1
Matrix no:  1
[(6.0, 7.0, 8.0), (6.0, 8.0, 2.0)]
[(1.0, 2.0, 3.0), (9.0, 0.0, 1.0)]
Enter the elements of Matrix:
7 4 8
5 2 8
9 3 6
0 0 0
Matrix no:  2
[(7.0, 4.0, 8.0), (5.0, 2.0, 8.0)]
[(9.0, 3.0, 6.0), (0.0, 0.0, 0.0)]
Enter the elements of Matrix:
1 2 3
5 6 7
8 4 3
8 6 4
Matrix no:  3
[(1.0, 2.0, 3.0), (5.0, 6.0, 7.0)]
[(8.0, 4.0, 3.0), (8.0, 6.0, 4.0)]
([(1.0, 2.0, 3.0), (5.0, 6.0, 7.0)], [(8.0, 4.0, 3.0), (8.0, 6.0, 4.0)])

Why I couldn't get the maximum value in tuples of the matrix in the above program? What should I do to get the maximum values in tuples of the matrix in the above program?

Suppose if I need the maximum value in tuples of the matrix 1 & 2, matrix 3 & 2, matrix 1 & 3 and matrix 1, 2 & 3. How to get the collection of all possible maximum matrices?

For example, I need the result to be

maximum value of tuples of matrix 1, 2 & 3

i.e.,

[(7.0, 7.0, 8.0), (6.0, 8.0, 8.0)]
[(9.0, 4.0, 6.0), (9.0, 6.0, 4.0)]

Likewise for matrix 1 & 2, matrix 3 & 2, matrix 1 & 3

  • 2
    Your question is not about entering data into a matrix, so take that out and use a hardcoded one. What is needed it a [mre] that illustrates the problem. It's also unclear how there could be more than one maximum value in the matrix, so please clarify what you mean. – martineau Jul 11 '22 at 02:29
  • 2
    Welcome to Stack Overflow. `tuple(map(lambda i, j: max(i ,j) , matrix_i, matrix_i))` This means: for each element in `matrix_i` (note: those are the **inner lists** like `[(6.0, 7.0, 8.0), (6.0, 8.0, 2.0)]`), figure out which is bigger: that element, *or the same element* (because `map` was given `matrix_i` to use for both the `i` values and the `j` values for the lambda. Then build a `tuple` of those results. Also, notice that *when that code runs*, `matrix_i` stores **only the last** matrix - every time through the `for i in range(0,m):` loop, the value of `matrix_i` is **replaced**. – Karl Knechtel Jul 11 '22 at 02:32
  • 2
    That said, it is impossible to understand the question. First off, like was said - just show one hard-coded matrix. Second - **what should the answer be** for that matrix? Why? According to what logic? What do you mean by "maximum values", plural? – Karl Knechtel Jul 11 '22 at 02:34
  • Dear @martineau I have edited the question kindly do check it. Since I needed the maximum value for all tuples of the matrix 1 & 2, matrix 3 & 2, matrix 1 & 3 and matrix 1, 2 & 3, I mentioned it as maximum values. And I am just a beginner and learning python for my research purpose. I am writing this coding for my research so the data can't be fixed hence the coding can't be done with a hardcoded one. Thank you for your comments. – Mershia Rabuni Jul 11 '22 at 03:01
  • Dear @KarlKnechtel I have edited the question kindly do check it. Since I needed the maximum value for all tuples of the matrix 1 & 2, matrix 3 & 2, matrix 1 & 3 and matrix 1, 2 & 3, I mentioned it as maximum values. And I am just a beginner and learning python for my research purpose. I am writing this coding for my research so the data can't be fixed hence the coding can't be done with a hardcoded one. Thank you for your comments. – Mershia Rabuni Jul 11 '22 at 03:03
  • What you are asking is much too complex to be considered a *single* question. – Karl Knechtel Jul 11 '22 at 03:29

2 Answers2

0

1.Why I couldn't get the maximum value in tuples of the matrix in the above program?

As @KarlKnechtel said, matrix_i stores only the last matrix, so you can't expect to get the maximum value using func max(). Besides, max(tuple1, tuple2) won't work as you expect. E.g.,max((2,4,1), (1,5,3)) will retrun (2,4,1) rather than (2,5,3). For more, you may refer to this.

2.What should I do to get the maximum values in tuples of the matrix in the above program?

The following code can satisify your need. If you need mat1 & mat2 & mat3, you only need to input 0, 1, 2 in turn when message "Enter the index of matrix to compute max value:" occurs.

while True:
    try:
        m = int(input("Number of matrices: "))
        break
    except:
        print('invalid input, please input again')
        continue
while True:
    try:
        rows = int(input("Enter the Number of rows : "))
        break
    except:
        print('invalid input, please input again')
        continue
while True:
    try:
        column = int(input("Enter the Number of Columns: "))
        break
    except:
        print('invalid input, please input again')
        continue

p = 0
matrix =[]
for i in range(0,m):
    print("Enter the elements of Matrix:")
    try:
        matrix.append([[tuple(map(float, input().split(" "))) for i in range(column)] for i in range(rows)])
    except:
        print('invalid input, please input again')
        continue
    p = p + 1
    print("Matrix no: ", p)
    for n in matrix[i]:
        print(n)


def max_matrix(mat1, mat2):
    res = mat1
    for i in range(column):
        for j in range(rows):
            res[i][j] = tuple(max(mat1[i][j][k], mat2[i][j][k]) for k in range(len(mat1[0][0])))
    return res


res = [[tuple(map(float, [0] * len(matrix[0][0][0]))) for i in range(column)] for i in range(rows)]

while True:
    try:
        i = int(input("Enter the index of matrix to compute max value:"))
        if i > len(matrix) - 1:
            print("out of index")
            continue
        if i == -1:
            break
    except:
        print('invalid input, please input again')
        continue
    res = max_matrix(res, matrix[i])
    print(res)
x pie
  • 552
  • 1
  • 10
0

I was wrong when I said your question was not about entering data into a matrix, because in a sense it is because that's one of the things you were not doing correctly. In your code all elements of the user's input for each matrix is getting stored in a single matrix named matrix_i by overwriting previous values, which makes it impossible to compare what is in one with another.

The following code shows how to enter data for multiple matrices and store them separately in a list named matrices so they can be compared to one another and printed out, etc as needed.

from pprint import pprint

m = int(input("Number of matrices: "))
rows = int(input("Enter the Number of rows : "))
columns = int(input("Enter the Number of Columns: "))

matrices = []
for i in range(m):
    print("Enter the elements of Matrix:")
    matrix_i = [[tuple(map(float, input().split(" "))) for c in range(columns)]
                    for r in range(rows)]
    print("Matrix no: ", i+1)
    for n in matrix_i:
        print(n)
    print()
    matrices.append(matrix_i)

pprint(matrices)

However for development and testing purposes it's useful to have a hardcoded set of values, so the data doesn't have to be reentered manually every time the code is run to test it.

def tuple_max(t1, t2):
    """Return tuple comprised of the maximum values in each tuple argument."""
    return tuple(map(max, tuple(zip(t1, t2))))

def matrix_max(m1, m2):
    """Return maxtrix of tuples comprised of maximum values for tuples in each row."""
    rows = []
    for r1, r2 in zip(m1, m2):
        cols = []
        for t1, t2 in zip(r1, r2):
            cols.append(tuple_max(t1, t2))
        rows.append(cols)
    return rows

for i, matrix in enumerate(matrices, start=1):
    print(f'm{i}: {matrix}')
print('----')

max12 = matrix_max(matrices[0], matrices[1])
print()
print('maximum value in tuples of matrix m1 and m2')
pprint(max12, width=40)

max23 = matrix_max(matrices[1], matrices[2])
print()
print('maximum value in tuples of matrices m2 and m3')
pprint(max23, width=40)

max13 = matrix_max(matrices[0], matrices[2])
print()
print('maximum value in tuples of matrices m1 and m3')
pprint(max13, width=40)

max123 = matrix_max(matrix_max(max12, max23), max13)
print()
print('maximum value in tuples of matrices m1, m2, and m3')
pprint(max123, width=40)

Here are the results that are printed using the hardcoded sample input. I think they show that what you want to do is getting done:

m1: [[(6.0, 7.0, 8.0), (6.0, 8.0, 2.0)], [(1.0, 2.0, 3.0), (9.0, 0.0, 1.0)]]
m2: [[(7.0, 4.0, 8.0), (5.0, 2.0, 8.0)], [(9.0, 3.0, 6.0), (0.0, 0.0, 0.0)]]
m3: [[(1.0, 2.0, 3.0), (5.0, 6.0, 7.0)], [(8.0, 4.0, 3.0), (8.0, 6.0, 4.0)]]
----

maximum value in tuples of matrix m1 and m2
[[(7.0, 7.0, 8.0), (6.0, 8.0, 8.0)],
 [(9.0, 3.0, 6.0), (9.0, 0.0, 1.0)]]

maximum value in tuples of matrices m2 and m3
[[(7.0, 4.0, 8.0), (5.0, 6.0, 8.0)],
 [(9.0, 4.0, 6.0), (8.0, 6.0, 4.0)]]

maximum value in tuples of matrices m1 and m3
[[(6.0, 7.0, 8.0), (6.0, 8.0, 7.0)],
 [(8.0, 4.0, 3.0), (9.0, 6.0, 4.0)]]

maximum value in tuples of matrices m1, m2, and m3
[[(7.0, 7.0, 8.0), (6.0, 8.0, 8.0)],
 [(9.0, 4.0, 6.0), (9.0, 6.0, 4.0)]]
martineau
  • 119,623
  • 25
  • 170
  • 301