简体   繁体   中英

Python: Matrix multiplication via array slicing

For an exercise of my programming class (we are using Python), I have to write a program that implements Matrix multiplication. The point is, that we should do this via array slicing and vector operations while trying to avoid using loops.

Unfortunately I have no idea how to do this without any loop. Using array slicing I could figure out a way to solve this with a loop nested within another loop instead of the direct way where you need three nested loops.

But how to do this without any loop?

Here is a sample code

import numpy as np 

# input two matrices 
mat1 = ([1, 6, 5],[3 ,4, 8],[2, 12, 3]) 
mat2 = ([3, 4, 6],[5, 6, 7],[6,56, 7]) 

# This will return dot product 
res = np.dot(mat1,mat2) 
//Output
[[ 63 320  83]
 [ 77 484 102]
 [ 84 248 117]]

Please read the docs for future reference rather than asking for help straightaway since the documentation for numpy and python libraries is expansive and well written.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM