开发者

How to optimize a matrix multiplication in MATLAB?

开发者 https://www.devze.com 2023-03-26 23:13 出处:网络
Can I somehow optimize this formula? I evaluate it many times and it takes much time... w - 1xN double

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

开发者_C百科

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消