Skip to content

Instantly share code, notes, and snippets.

@stevenrouk
Created August 25, 2019 23:13
Show Gist options
  • Select an option

  • Save stevenrouk/12720999be59396750cb41d5df75e3e9 to your computer and use it in GitHub Desktop.

Select an option

Save stevenrouk/12720999be59396750cb41d5df75e3e9 to your computer and use it in GitHub Desktop.
import numpy as np
def for_loop_matrix_multiplication4(A, B):
"""Fourth version of a for loop matrix multiplication.
In this version, we replace B.T with zip(*B) in order to
transpose B without needing to convert it to a NumPy array first.
This means we can remove the opening np.array conversion lines too.
"""
new_matrix = []
for i, row in enumerate(A):
new_row = []
for j, col in enumerate(zip(*B)):
dot_product = sum([x*y for (x, y) in zip(row, col)])
new_row.append(dot_product)
# Now, we need to append the new_row to our new_matrix
# before moving on to the next row in A.
new_matrix.append(new_row)
return new_matrix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment