Tuesday, April 5, 2022

Matrix

 Matrix in python:

1) take input matrix :


    m=[]

    r,c=map(int,input().split())

    for i in range(r):

        a=[]

        for j in range(c):

            a.append(int(input()))

        m.append(a)

    print(m)

    output:

            2 4

            

            2

            3

            4

            5

            6

            7

            8

            [[1, 2, 3, 4], [5, 6, 7, 8]]


                                    OR

           r,c=map(int,input().split())

        m=[[int(input()) for i in range(r)] for j in range(c)]

        print(m)

                                            OR

    R = int(input("Enter the number of rows:"))

    C = int(input("Enter the number of columns:"))

    matrix = []

    print("Enter the entries rowwise:")

    for i in range(R):  

a =[]

for j in range(C):  

a.append(int(input()))

matrix.append(a)

    for i in range(R):

for j in range(C):

print(matrix[i][j], end = " ")

print()


                                             OR

        

        import numpy as np

        r,c=map(int,input().split())

        l=list(map(int,input().split()))

        m=np.array(l).reshape(r,c)

        print(m)






No comments:

Post a Comment