Thread Subject: about scalar and Sparse Matrix multiplication

Subject: about scalar and Sparse Matrix multiplication

From: Shiyuan Gu

Date: 19 Jan, 2010 05:40:06

Message: 1 of 3

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

Subject: about scalar and Sparse Matrix multiplication

From: James Tursa

Date: 19 Jan, 2010 06:45:04

Message: 2 of 3

"Shiyuan Gu" <gshy2014@gmail.com> wrote in message <hj3gjm$9p5$1@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

Subject: about scalar and Sparse Matrix multiplication

From: Bruno Luong

Date: 19 Jan, 2010 07:35:05

Message: 3 of 3

"Shiyuan Gu" <gshy2014@gmail.com> wrote in message <hj3gjm$9p5$1@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

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
efficiency spar... Shiyuan Gu 19 Jan, 2010 00:44:04
rssFeed for this Thread

Contact us at files@mathworks.com