Sort by

recency

|

8 Discussions

|

  • + 0 comments
    A = [[1,2],[2,3]]
    B = [[4,5],[7,8]]
    
    ROWS_A, COLS_A = len(A), len(A[0])
    ROWS_B, COLS_B = len(B), len(B[0])
    assert COLS_A == ROWS_B
    COMMON_D = ROWS_B
    
    # zeros for Rows-A x Cols-B
    M_TIMES = []
    for r in range(ROWS_A):
        M_TIMES.append( [0] * COLS_B )
    
    # Loop over Rows-A, then Cols-B, then the common dimension
    for rA in range(ROWS_A):
        for cB in range(COLS_B):
            for t in range(COMMON_D):
                M_TIMES[rA][cB] += A[rA][t]*B[t][cB]
            print(M_TIMES[rA][cB])
    
  • + 0 comments
    a = [1, 2], [2, 3]
    b = [4, 5], [7, 8]
    res = 0 
    for i in range(2):
            for j in range(2):
                    for k in range(2):
                            res += a[i][k]*b[k][j]           
                    print(res)
                    res = 0
    
  • + 1 comment

    Python3:

    # Enter your code here. Read input from STDIN. Print output to STDOUT
    if __name__ == "__main__":
      matrix1 = [[1, 2], [2, 3]]
      matrix2 = [[4, 5], [7, 8]]
      elem = 0
      
      for i in range(len(matrix1)): # row in 1st matrix
        for j in range(len(matrix2[0])): # column in 2nd matrix
            for k in range(len(matrix2)): # position index
                elem += matrix1[i][k] * matrix2[k][j]
            print(elem)
            elem = 0
    

    or

    if __name__ == "__main__":
        M, N = [[1, 2], [2, 3]], [[4, 5], [7, 8]]
        
        lst = []
        for row1m in M:
            for j in range(len(row1m)-1):
                lst += [row1m[j]*N[0][0] + row1m[j+1]*N[1][0]]
                lst += [row1m[j]*N[0][1] + row1m[j+1]*N[1][1]]  
        print(*lst, sep='\n')
    
  • + 0 comments
    a1 = [1,2]
    a2 = [2,3]
    
    
    matrix_a = [a1,a2]
    
    b1 = [4,5]
    b2 = [7,8]
    
    
    matrix_b = [b1,b2]
    
    c1 = [0, 0]
    c2 = [0, 0]
    
    
    matrix_c = [c1,c2]
    
    for i in range(len(matrix_a)):
       for j in range(len(matrix_b[0])):
           for k in range(len(matrix_b)):
               matrix_c[i][j] += matrix_a[i][k] * matrix_b[k][j]
    
    for n in range(len(matrix_c)):
      for i in range(len(matrix_c[n])):
        print(matrix_c[n][i])
    
  • + 0 comments
    18
    21
    29
    34