Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Fast access to rows and columns of sparse matrices

Subject: Fast access to rows and columns of sparse matrices

From: Franz-Josef

Date: 23 May, 2010 08:15:30

Message: 1 of 7

Hello everybody!
My question is about sparse matrices.
For my FE-Program, written in Matlab, I want to set values of
certain columns and rows of a sparse matrix zero. For example:
K(c,:) = 0, where c is a vector. If K is a full matrix, this operation
takes only a second for matrix sizes up to 10000. If K is a sparse
matrix this operation takes over one hour!
What possibilities exist, to speed up this operation with sparse matrices?
Thanks a lot for your suggestions!
FJ

Subject: Fast access to rows and columns of sparse matrices

From: James Tursa

Date: 23 May, 2010 09:26:03

Message: 2 of 7

"Franz-Josef " <franz.falkner@uibk.ac.at> wrote in message <htao72$etb$1@fred.mathworks.com>...
> Hello everybody!
> My question is about sparse matrices.
> For my FE-Program, written in Matlab, I want to set values of
> certain columns and rows of a sparse matrix zero. For example:
> K(c,:) = 0, where c is a vector. If K is a full matrix, this operation
> takes only a second for matrix sizes up to 10000. If K is a sparse
> matrix this operation takes over one hour!
> What possibilities exist, to speed up this operation with sparse matrices?
> Thanks a lot for your suggestions!
> FJ

What version of MATLAB are you using & what system are you running on?

James Tursa

Subject: Fast access to rows and columns of sparse matrices

From: Loren Shure

Date: 25 May, 2010 17:09:54

Message: 3 of 7

In article <htao72$etb$1@fred.mathworks.com>, franz.falkner@uibk.ac.at
says...
> Hello everybody!
> My question is about sparse matrices.
> For my FE-Program, written in Matlab, I want to set values of
> certain columns and rows of a sparse matrix zero. For example:
> K(c,:) = 0, where c is a vector. If K is a full matrix, this operation
> takes only a second for matrix sizes up to 10000. If K is a sparse
> matrix this operation takes over one hour!
> What possibilities exist, to speed up this operation with sparse matrices?
> Thanks a lot for your suggestions!
> FJ
>

Because of the way sparse matrices are stored (see info in doc), you are
far better off speed-wise working with columns instead of rows. Perhaps
that will work for you, but I can't tell.

--
Loren
http://blogs.mathworks.com/loren
http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: Fast access to rows and columns of sparse matrices

From: Tim Davis

Date: 27 May, 2010 23:40:23

Message: 4 of 7

"Franz-Josef " <franz.falkner@uibk.ac.at> wrote in message <htao72$etb$1@fred.mathworks.com>...
> Hello everybody!
> My question is about sparse matrices.
> For my FE-Program, written in Matlab, I want to set values of
> certain columns and rows of a sparse matrix zero. For example:
> K(c,:) = 0, where c is a vector. If K is a full matrix, this operation
> takes only a second for matrix sizes up to 10000. If K is a sparse
> matrix this operation takes over one hour!
> What possibilities exist, to speed up this operation with sparse matrices?
> Thanks a lot for your suggestions!
> FJ

Try this instead:

K=K' ;
K(:,c) = 0;
K=K' ;

See http://blogs.mathworks.com/loren/2007/03/01/creating-sparse-finite-element-matrices-in-matlab/ for details.

Subject: Fast access to rows and columns of sparse matrices

From: James Tursa

Date: 28 May, 2010 06:30:12

Message: 5 of 7

"Franz-Josef " <franz.falkner@uibk.ac.at> wrote in message <htao72$etb$1@fred.mathworks.com>...
> Hello everybody!
> My question is about sparse matrices.
> For my FE-Program, written in Matlab, I want to set values of
> certain columns and rows of a sparse matrix zero. For example:
> K(c,:) = 0, where c is a vector. If K is a full matrix, this operation
> takes only a second for matrix sizes up to 10000. If K is a sparse
> matrix this operation takes over one hour!
> What possibilities exist, to speed up this operation with sparse matrices?
> Thanks a lot for your suggestions!
> FJ

Can you give more details about the exact size and sparsity of your K matrix? I have tried 100MB matrices built with sprand and it only takes a couple of seconds to zero out a single row, and a few 10's of seconds to zero out 1000 rows. How big is your c vector?

James Tursa

Subject: Fast access to rows and columns of sparse matrices

From: H

Date: 28 May, 2010 07:14:04

Message: 6 of 7

Try this:

http://www.mathworks.com/matlabcentral/fileexchange/23488-sparse-sub-access

Worked like a charm for me when I had the exact same problem...

Subject: Fast access to rows and columns of sparse matrices

From: Pat Quillen

Date: 30 May, 2010 01:11:04

Message: 7 of 7

"Franz-Josef " <franz.falkner@uibk.ac.at> wrote in message <htao72$etb$1@fred.mathworks.com>...
> Hello everybody!
> My question is about sparse matrices.
> For my FE-Program, written in Matlab, I want to set values of
> certain columns and rows of a sparse matrix zero. For example:
> K(c,:) = 0, where c is a vector. If K is a full matrix, this operation
> takes only a second for matrix sizes up to 10000. If K is a sparse
> matrix this operation takes over one hour!
> What possibilities exist, to speed up this operation with sparse matrices?
> Thanks a lot for your suggestions!
> FJ

FJ:

With MATLAB 7.10 (R2010a) assignment into rows of sparse matrices is generally much faster than in previous releases. For example, on a 64 bit Linux machine, R2009b gives:

>> version
ans =
7.9.0.529 (R2009b)
>> A = sprand(10000,10000,0.01);
>> p = randi(10000,1,1000);
>> tic; A(p,:) = 0; toc
Elapsed time is 3.750562 seconds.

On the other hand, we see for R2010a:

>> version
ans =
7.10.0.499 (R2010a)
>> A = sprand(10000,10000,0.01);
>> p = randi(10000,1,1000);
>> tic; A(p,:) = 0; toc
Elapsed time is 0.055481 seconds.

Of course, the amount of time the operation takes is proportional to the size of the matrix and the number of nonzeros, but the R2010a version is much improved. The improvement is not limited to assigning in scalars:

>> version
ans =
7.9.0.529 (R2009b)
>> A = sprand(10000,10000,0.01);
>> p = randi(10000,1,1000);
>> B = sprand(1000,10000,0.1);
>> tic; A(p,:) = B; toc
Elapsed time is 5.701408 seconds.

>> version
ans =
7.10.0.499 (R2010a)
>> A = sprand(10000,10000,0.01);
>> p = randi(10000,1,1000);
>> B = sprand(1000,10000,0.1);
>> tic; A(p,:) = B; toc
Elapsed time is 0.124965 seconds.

Echoing other posters, one should always favor working columnwise in MATLAB (both with sparse and dense matrices alike) but we are working hard to make natural expressions (such as A(p,:) = B) have the best possible performance. Look for further improvements to the performance of sparse matrix indexing with coming releases.

Finally, it is worth noting that assigning zeros into a sparse matrix will likely be slower than assigning zeros into a dense matrix of the same size since in the case of the sparse matrix, elements typically have to be moved around to squeeze out newly introduced zeros.

Take care.
Pat.

Tags for this Thread

No tags are associated with this thread.

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.

Contact us