def Matrix_multiply(A,B):
if len(A[0])!=len(B):
return "error"
C=[[0 for j in range( len(A[0]))] for i in range( len(B))]
for i in range(len(A[0])):
for j in range(len(B)):
C[i][j]=0
for k in range(len(A)):
C[i][j]=C[i][j]+A[i][k]*B[k][j]
print(C[i][j])
return C
A=[[1,2],[3,4]]
B=[[1,2],[3,4]]
Matrix_multiply(A,B)
import numpy as np
A =np.array([[1,2,3]])
B= np.array([[1,2,3],[3,4,1],[3,4,1]])
print(np.multiply(B,A),"numpy")
def Matrix_multiply(A,B):
if A.shape[1]!=B.shape[0]:
return "error"
c=d=np.ones((1,3))
for i in range(A.shape[0]):
for j in range(B.shape[1]):
c[i,j]=0
for k in range (A.shape[1]):
c[i,j]=c[i,j]+A[i,k]*B[k,j]
return c
print(Matrix_multiply(A,B))
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)