Created
August 31, 2015 12:25
-
-
Save andriisoldatenko/44d1dc960308617dd170 to your computer and use it in GitHub Desktop.
matrix_multiply
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
A = [[1, 2, 3], | |
[1, 2, 3], | |
[1, 2, 3], | |
[1, 2, 3]] | |
B = [[1, 2, 3, 4], | |
[1, 2, 3, 4], | |
[1, 2, 3, 4]] | |
def multiply(A, B): | |
A_rows = len(A) | |
A_cols = len(A[0]) | |
B_rows = len(B) | |
B_cols = len(B[0]) | |
if A_cols != B_rows: | |
print "Can\'t multiply" | |
return | |
C = [] | |
for x in range(A_rows): | |
C.append(list()) | |
for y in range(B_cols): | |
C[x].append(0) | |
print C | |
for i in range(A_rows): | |
for j in range(B_rows): | |
for el in range(A_cols): | |
C[i][j] += A[i][j] + B[el][j] | |
print C | |
if __name__ == '__main__': | |
multiply(A, B) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment