Apr-29-2019, 09:40 PM
This code is supposed to multiply a matrix by a vector without the aid of a .dot function.
In order to make sure I get the right result, I use the @ command right after printing the function.
This code prints
[7, 15, 24]
when using the function
and
[ 6 15 24]
when using the dot product command.
Can anyone please tell me where this is wrong?
Why does the function print 7 instead of 6?
In order to make sure I get the right result, I use the @ command right after printing the function.
This code prints
[7, 15, 24]
when using the function
and
[ 6 15 24]
when using the dot product command.
Can anyone please tell me where this is wrong?
Why does the function print 7 instead of 6?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
def matvec1(a,b): if a.shape[ 0 ] = = b.shape[ 0 ]: for i in range ( len (a)): newarray = [] s = 0 for j in range ( len (b)): s + = a[i][j] * b[i] newarray.append(s) return newarray else : return "The matrix and vector aren't compatible." A1 = np.array([[ 1 , 2 , 3 ],[ 4 , 5 , 6 ],[ 7 , 8 , 9 ] ]) B1 = np.array([ 1 , 1 , 1 ]) print (matvec1(A1,B1)) print (A1@B1) |