Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

about scalar and Sparse Matrix multiplication

2 views
Skip to first unread message

Shiyuan Gu

unread,
Jan 19, 2010, 12:40:06 AM1/19/10
to
I met a strange problem about the scalar and Sparse Matrix Multiplication.
The following expression:
v=0.25*M*w
is equivalent to
v=M*w;
v=0.25*v;
where M is a sparse matrix
However,
the latter is much faster way than the former way (about ten time). Why?
Thanks

James Tursa

unread,
Jan 19, 2010, 1:45:04 AM1/19/10
to
"Shiyuan Gu" <gshy...@gmail.com> wrote in message <hj3gjm$9p5$1...@fred.mathworks.com>...

Depends on what w is, what the sizes of the variables are, and what MATLAB version you are running. If you do the M*w first then the result could be a lot smaller than M, so the subsequent multiply by 0.25 is not as much as doing the 0.25 multiply first. Also, different versions of MATLAB handle the scalar * sparse multiply differently, so you can get significantly different timing answers on different machines with different versions of MATLAB.

James Tursa

Bruno Luong

unread,
Jan 19, 2010, 2:35:05 AM1/19/10
to
"Shiyuan Gu" <gshy...@gmail.com> wrote in message <hj3gjm$9p5$1...@fred.mathworks.com>...

The first version does this
v = (0.25*M)*w

The reason why it's slower is now obvious: in one case the scalar is multiplied with a matrix; in other case the scalar is multiplied with a vector.

Bruno

0 new messages