matrix multiplication

Seb spluque at gmail.com
Sun Feb 25 12:45:51 EST 2018


Hello,

The following is an example of an Nx3 matrix (`uvw`) representing N
vectors that need to be multiplied by a 3x3 matrix (generated by
`randint_mat` function) and store the result in `uvw_rots`:

---<--------------------cut here---------------start------------------->---
import numpy as np


def randint_mat(x):
    return np.random.randint(x, size=(3, 3))


np.random.seed(123)
uvw = np.random.randn(1000, 3)
maxint = np.random.randint(1, 10, size=uvw.shape[0])

uvw_rots = np.empty_like(uvw)
for i, v in enumerate(maxint):
    mati = randint_mat(v)
    uvw_roti = np.dot(uvw[i], mati)
    uvw_rots[i] = uvw_roti
---<--------------------cut here---------------end--------------------->---

Is there a better (faster) approach than looping through the rows of uvw
as shown?

Thanks,
-- 
Seb




More information about the Python-list mailing list