Sparse Matrix Gather Operation

An implementation of something similar to BLAS gather (where sparse() is similar to scatter)
Updated 9 Feb 2007

No License



then this function is the "inverse" of sparse, in the sense that


For small matrices, an equivalent (and more efficient) version is


This version breaks down for matrices for which prod(size) is bigger than 2^31 or so. For square matrices, this limits the size to about 30,000 by 30,000. For purposes such as finite elements, a 30,000 by 30,000 sparse matrix is actually small. The gather operation is mostly not needed except in the mesh subdivision routine, which needs to be able to look up pairs of vertices in a sparse matrix to recover an edge identifier (so that these edges can be subdivided.)

In this situation, for large meshes, the only way I found of not overflowing the 31 bit integers is to use this implementation of the gather operation.

The performance is reasonably good. Because one is reading from a sparse matrix, each access can be expected to take log(n) time (n=size(S,1)). For m accesses, the running time should therefore be mlogn. The associated screenshot is a performance measurement. The running time is measured by


The measurement is compared to nlogn/5e5. The two curves are very close, so this algorithm is sound.

Cite As

Sébastien Loisel (2024). Sparse Matrix Gather Operation (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2006a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Find more on Sparse Matrices in Help Center and MATLAB Answers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes

Corrected code in description. This includes the changes I made in the previous update.