From: Shiyuan Gu on
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
From: James Tursa on
"Shiyuan Gu" <gshy2014(a)gmail.com> wrote in message <hj3gjm$9p5$1(a)fred.mathworks.com>...
> 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

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
From: Bruno Luong on
"Shiyuan Gu" <gshy2014(a)gmail.com> wrote in message <hj3gjm$9p5$1(a)fred.mathworks.com>...
> 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

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