View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Sparse sub access

5.0 | 11 ratings Rate this file 18 Downloads (last 30 days) File Size: 25.8 KB File ID: #23488 Version: 1.11

Sparse sub access


Bruno Luong (view profile)


30 Mar 2009 (Updated )

This package allows to retrieve and assign values of sparse matrix in one shot.

| Watch this File

File Information

The indexes must be provided in (row,column) form. Functions in this package are programmed with care so no overflow of linear index is used with large-size matrix. I try to design an engine of automatic selection algorithms so as to provide a good run time performance across various cases. I'm not sure it's worth the effort, but my experience shows that it is easy to mess with the performance when non-appropriate method is using in sparse manipulation. This package does not warranty for best performance, but it just has a modest pretension to prevent user to use bad algorithm. For a better customize solution, a calibration is required on the specific
computer by running this command: spcalib('auto')


This file inspired Robust Sparse Data Types.

MATLAB release MATLAB 7.7 (R2008b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (19)
17 Sep 2014 Christoforos Rekatsinas

Got it thx!!

17 Sep 2014 Christoforos Rekatsinas

Im trying to build a large FE model with this package but i dont get any advantage on the speed.

shuangou ren how exactly do u use it one FE assemblage

Comment only
10 Apr 2013 Denis Anikiev

10 Oct 2012 Maarten Manders

23 Feb 2012 Hannu K

For more confusion, matlab version 2010a didn't crash in the example above. The 2011b on the otherhand, did.

Comment only
23 Feb 2012 Hannu K

This fantastics submission seems to have sort of a bug. I accidentally did the followin: B = setsparse(A,inf,inf,1).
This didn't result in a problem before my function ended and tried to clear the variable B. I know that this is strictly the user's problem but a single check if the indexes are suitable could be useful for other unfortunate fools...

23 Nov 2010 LeFlaux

LeFlaux (view profile)

Hey Bruno!

In my FE-Code the global matrices are assembled as usual in a loop over the elements, eg:

A(idx, idx) = A(idx, idx) + locA;

where idx defines a sub-matrix of A (eg idx = [3,1,4,12,7,10] and locA is 6x6). I translated this code to Your functions as follows using row and columns indexing:

[cidxs, ridxs] = meshgrid(idxs, idxs);
A = setsparse(A, ridxs(:), cidxs(:), locA(:), @plus);

Unfortunately, this is (little) more expensive than the original version. Do You have any suggestions how to shorten things above a little?

Thank you in anticipation for your support.

19 Nov 2010 zhuhhu

zhuhhu (view profile)

good job, two times faster for finite element assemblage.

19 Nov 2010 zhuhhu

zhuhhu (view profile)

04 Mar 2010 Bruno Luong

Bruno Luong (view profile)

Hello H,

I can't really help with memory problem. The suggestion I can give is to try on your side to increase the free largest memory block from other places. Thanks for the kind words.


Comment only
03 Mar 2010 H

H (view profile)

This seems to have one problem though:

Before I used A(:,indexes)=0, it just took a lot of time.

No I tried

[I J]=find(A);
idxofzero = ismember(J,indexes);

And I get "out of memory error". I tried it several times with both methods and got the same result. Am I missing something here?

My sparse matrix is about 55,000 x 55,000 and it has 4,060,200 nnz elements. The indexes hold about 1/100 of the columns.

Comment only
03 Mar 2010 H

H (view profile)

Bruno, Thank you very much for this package. Also thank you for all your help in the forums.

This works appr. 30 times faster than using linear indexing to zero sparse matrix columns that I used before!

11 Jan 2010 Bruno Luong

Bruno Luong (view profile)

Thanks for the tip James.

Comment only
11 Jan 2010 James Tursa

James Tursa (view profile)

FYI, my experience is that lcc has bugs with the unsigned long long type ... I often get a "bad rule" compile error. I have gotten into the habit of using signed long long instead and writing extra code to handle situations where I need to shift into or out of the sign bit. A real hassle just so the lcc compiler that ships with MATLAB will work with my code.

31 Dec 2009 Matt J

Matt J (view profile)

Yep, that workaround fixed it. Anyway, I like it!!

16 Dec 2009 Bruno Luong

Bruno Luong (view profile)

Hello Matt,

After investigation, it turns out the "_kids--Bad rule" message is caused by a bug of the LCC compiler.

A workaround is to replace the or "|" operator by plus "+" operator in the cmex file setspvalmex.c, lines #626, 634 and 709, then recompile.

Thanks for report the bug. I'll submit a new version.


Comment only
16 Dec 2009 Matt J

Matt J (view profile)

Bruno - When I do

mex -v -O setspvalmex.c

I get a compilation error:

Error setspvalmex.c: 626 compiler error in _kids--Bad rule number 0

C:\PROGRA~1\MATLAB\R2009B\BIN\MEX.PL: Error: Compile of 'setspvalmex.c' failed.

Any idea what I'm doing wrong?

Comment only
01 Aug 2009 Anthony Lamb

Sorry about that.... I think that the following code does the trick

A_global = setspvalmex(A_global, Ic, Jr, X,@plus);

Comment only
01 Aug 2009 Anthony Lamb

Very good!!!! It works well for building global stiffness matrices for finite element computations. While building my stiffness matrices I currently do the following
val = getspvalmex(A_global, Ic, Jr);
A_global = setspvalmex(A_global, Ic, Jr, val+X);

Bruno...Is there any other way your code can be used to get this done??

31 Mar 2009 1.1

Add a feature of bi-variate function-handle when assign values

01 Apr 2009 1.2

Adjust the maximum of linear index allowed according to bug information communicated by The Mathworks (applicable only for 32-bit platform, v.2009A or previous)

03 Apr 2009 1.3

A brand new mex implementation that overcome Matlab indexing Bug (32 bit platform) and improve the speed by approximately a factor of 4.

06 Apr 2009 1.4

More comprehensive calibration
Correct mex-build command for 64-bit platform
Many small improvements and bug fixes

11 Apr 2009 1.5

A new mex sparse assign engine is developped from scratch using quicksort. Finally, we beat adding routine by about 40% of speed. More comprehensive calibration routine is included.

15 Apr 2009 1.6

Correct a bug in setspvalmex.c, which concerns 64-bit only

17 Apr 2009 1.7

Correct bug when calling setspvalmex.c with empty indice.
This code is also ready to compiled using INTROSORT, however QUICKSORT is still retained as default engine.

16 Dec 2009 1.8

Workaround for BUG of LCC (related to "|" operator in expression with int64)

10 Jan 2010 1.9

switch the sort-engine of setsparse to introsort which is more stable than quicksort (previously used) in term of complexity

13 Nov 2010 1.11

When passing scalar row or column, it will automatically expanded to match the other

Contact us