简体   繁体   中英

How to optimize a matrix multiplication in MATLAB?

Can I somehow optimize this formula? I evaluate it many times and it takes much time...

w - 1xN double

phis - NxN double

x - Nx2 double

sum(w(ones([size(x, 1) 1]),:).* phis, 2)

You're taking the scalar product of each row of phis with w . You can do this easily using linear algebra.

out = phis * w';

This matrix multiplication saves you calls to sum , ones , and size , which should make your code a lot faster. Furthermore, linear algebra operations are often very fast in Matlab, since that's what the program is historically optimized for.

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