COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### What is the proper way to read/write sparse matrix?

• Email
• Follow

```Hi All,

I have a simple piece of code to work with two sparse matrices: read each column of one matrix and write values to another sparse matrix. I read from column 1 to end and write from column 1 to end. I may have 1e5 columns to read.

The speed is pretty quick to handle the first several thousands and then the speed is slow down significantly. The strange thing is, the speed can be fast if I read the sparse matrix inversely: from column end to 1.

Is there any special suggestion on how to access MATLAB's sparse matrix?

Thanks,
Mathew
```
 0

See related articles to this posting

```"Mathew " <jhwang2k@gmail.com> wrote in message <hom87s\$12u\$1@fred.mathworks.com>...
> Hi All,
>
> I have a simple piece of code to work with two sparse matrices: read each column of one matrix and write values to another sparse matrix. I read from column 1 to end and write from column 1 to end. I may have 1e5 columns to read.
>
> The speed is pretty quick to handle the first several thousands and then the speed is slow down significantly. The strange thing is, the speed can be fast if I read the sparse matrix inversely: from column end to 1.
>
> Is there any special suggestion on how to access MATLAB's sparse matrix?
>
> Thanks,
> Mathew

When you read from 1 to end without preallocating, MATLAB has to continually reallocate memory and copy memory over and over again. By reading from end to 1, you essentially preallocate some of the sparse matrix and end up saving some of that wasted reallocation and copying, hence it runs faster. If you know the size ahead of time, try the following test: Suppose your matrix is X and you know it will be M x N. Set X(M,N) = 0 ahead of time and then read the rest in from 1 to end. Does this run as fast as not explicitly allocating ahead of time but reading from end to 1?

James Tursa
```
 0

1 Replies
469 Views

Similar Articles

12/6/2013 6:05:08 AM
page loaded in 28614 ms. (0)