Close
Close

add two matrix by inputing data from keyboard

   sreeram

program for addition of matrix

multiplication

and transpose


Answers

  •   
    #getting dimension of matrix
    
    print "enter n for nxn matrix"
    n = input()
    
    matrix1 = []
    matrix2 = []
    
    #taking elements of first matrix
    
    print "Enter elements of first matrix"
    for i in range(0,n):
    
      #taking elements of first column
    
      print "Enter elements of ",i,"column, seperated by space"
    
      #raw_input().split() will split the string
      #'1 2 34'.split() will give ['1', '2', '34']
      #map will convert its elements into integers [1, 2, 34]
    
      matrix1.append(map(int,raw_input().split()))
    
    print "Matrix 1 is",matrix1
    
    #taking elements of second matrix
    
    print "Enter elements of second matrix"
    for i in range(0,n):
      #Similar to input taken for 1 matrix
    
      print "Enter elements of ",i,"column, seperated by space"
      matrix2.append(map(int,raw_input().split()))
    
    print "Matrix 2 is",matrix2
    
    #adding
    
    add_matrix = []
    for i in range(0,n):
      a = []
      for j in range(0,n):
    
    
        #making a addition matrix's column to append
        #making a 1D matrix with elements as sum of elements of
        #respective columns of both matrices
    
        a.append(matrix1[i][j]+matrix2[i][j])
      add_matrix.append(a)
    print "Addition of matrix is",add_matrix
    
    #multiplication
    multi_matrix = []
    for i in range(0,n):
      a = []
      for j in range(0,n):
        summ = 0
        for k in range(0,n):
          summ = summ+(matrix1[i][k]*matrix2[k][j])
        a.append(summ)
      multi_matrix.append(a)
    print "matrix1 x matrix 2 =",multi_matrix
    
    #transpose of matrix1
    
    tr_matrix=[]
    
    for i in range(0,n):
      a = []
      for j in range(0,n):
    
        #matrix1[j][i] will give row of matrix 1
        #we are making it column of new matrix
        a.append(matrix1[j][i])
      tr_matrix.append(a)
    
    print "Transpose of matrix 1 is",tr_matrix
    

    Sample input and output

    enter n for nxn matrix
    3
    Enter elements of first matrix
    Enter elements of  0 column, seperated by space

    4 5 8
    Enter elements of  1 column, seperated by space
    9 3 1
    Enter elements of  2 column, seperated by space
    5 8 9
    Matrix 1 is [[4, 5, 8], [9, 3, 1], [5, 8, 9]]
    Enter elements of second matrix
    Enter elements of  0 column, seperated by space

    7 8 9
    Enter elements of  1 column, seperated by space
    5 6 8
    Enter elements of  2 column, seperated by space
    7 2 6
    Matrix 2 is [[7, 8, 9], [5, 6, 8], [7, 2, 6]]
    Addition of matrix is [[11, 13, 17], [14, 9, 9], [12, 10, 15]]
    matrix1 x matrix 2 = [[109, 78, 124], [85, 92, 111], [138, 106, 163]]
    Transpose of matrix 1 is [[4, 9, 5], [5, 3, 8], [8, 1, 9]]



  •    bilon83361

    View idm crack's profile https://pcwonderland.cc/ on LinkedIn, the world's largest professional community. idm has 1 job listed on their profile. See the complete profile on ... 



Ask Yours
Post Yours
Write your answer