Date: Jun 18, 1996 11:50 AM
Author: John D'Errico
Subject: sparse matrix bug

An interesting little bug I tripped over in Matlab on the Sun
today. It is also in the mac version.

Matlab is supposed to work with sparse matrices and full matrices
together, leaving the result as a sparse matrix.

For example, when A is sparse and B is full,


C is supposed to be sparse. This is good.


C is still supposed to be sparse. Still good.

Consider the following expression though.

C = A(:,ind) - B*(R1\R2);

A and B are sparse. ind is an index vector (full). R1 and R2
are full. C should be sparse, since it is a sum of terms,
all of which should be sparse.

NOT so. At least not on a Mac running 4.2c1 or on a sparc5.

I can fix it, by forcing R1 and R2 to be sparse, but this
cost me some headaches. When I later tried to use C, which is
huge when full, it crashed the sparc20 I was running it on
remotely with memory problems. It did not return cleanly. I
tried it again on my own sparc5, and it quickly crashed that
system too. No out of memory message. Just reboot.

If you do not believe me, try out the following code.
Check out b after it is computed. Full. Not sparse.

John D'Errico,

a = sprandn(100,50,0.1);
c = sprandn(10,50,0.05);