Skip to content

Matrix multiplication fails for matrix that are not Square matrix #837

Closed
@sandeepsj

Description

@sandeepsj

Currently The Matrix Multiplication function can multiply only square matrices
current code is
def multiply(matrix_a, matrix_b): matrix_c = [] n = len(matrix_a) for i in range(n): list_1 = [] for j in range(n): val = 0 for k in range(n): val = val + matrix_a[i][k] * matrix_b[k][j] list_1.append(val) matrix_c.append(list_1) return matrix_c

This could be changed to
 def multiply(matrix_a, matrix_b):
    matrix_c = []
    n = len(matrix_a)       #no of rows in Mat_a
    m = len(matrix_a[0])    #no of cols in Mat_a
    l = len(matrix_b[0])    #no of cols in Mat_b
    for i in range(n):
        list_1 = []
        for j in range(l):
            val = 0
            for k in range(m):
                val = val + matrix_a[i][k] * matrix_b[k][j]
            list_1.append(val)
        matrix_c.append(list_1)
    return matrix_c

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions