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:
sparse matrix multiplication: bad in Linux v7.5, good in Windows v7.1

Subject: sparse matrix multiplication: bad in Linux v7.5, good in Windows v7.1

From: Akim

Date: 20 Jan, 2009 04:15:04

Message: 1 of 5

Dear all,

My Matlab v7 (2005) matlab manual tells me that "Multiplication and division are performed on only the nonzero elements of sparse matrices". Such a set-up is very convenient for me.

On my older Windows matlab 7.1.0.124 (R14) (August 2005), multiplication (division) works just like the manual says it should:

EDU>> sparse([1 0 0 0 0])./0
ans =
   (1,1) Inf

But on a newer Linux matlab 7.5.0.338 (R2007b), the zero elements are also involved -- this is a big nuisance:

>> sparse([1 0 0 0 0])./0
ans =
   (1,1) Inf
   (1,2) NaN
   (1,3) NaN
   (1,4) NaN
   (1,5) NaN

How could I revert to the old version of multiplication? If that's not possible, how could I use spfun with two matrix inputs ... spfun(@times,[2 input matrices?]).

Many thanks for your help.

Subject: sparse matrix multiplication: bad in Linux v7.5, good in Windows v7.1

From: Bruno Luong

Date: 20 Jan, 2009 07:59:04

Message: 2 of 5


>
> how could I use spfun with two matrix inputs ... spfun(@times,[2 input matrices?]).
>

times(sa,sb) %???

Bruno

Subject: sparse matrix multiplication: bad in Linux v7.5, good in Windows v7.1

From: Steven Lord

Date: 20 Jan, 2009 15:04:53

Message: 3 of 5


"Akim " <aaa@bbb.ccc> wrote in message
news:gl3j48$hc4$1@fred.mathworks.com...
> Dear all,
>
> My Matlab v7 (2005) matlab manual tells me that "Multiplication and
> division are performed on only the nonzero elements of sparse matrices".
> Such a set-up is very convenient for me.
>
> On my older Windows matlab 7.1.0.124 (R14) (August 2005), multiplication
> (division) works just like the manual says it should:
>
> EDU>> sparse([1 0 0 0 0])./0
> ans =
> (1,1) Inf
>
> But on a newer Linux matlab 7.5.0.338 (R2007b), the zero elements are also
> involved -- this is a big nuisance:
>
>>> sparse([1 0 0 0 0])./0
> ans =
> (1,1) Inf
> (1,2) NaN
> (1,3) NaN
> (1,4) NaN
> (1,5) NaN
>
> How could I revert to the old version of multiplication? If that's not
> possible, how could I use spfun with two matrix inputs ... spfun(@times,[2
> input matrices?]).

You can't revert to the old behavior, as it was a bug that was fixed in
MATLAB 7.2 (R2006a):

http://www.mathworks.com/support/bugreports/details.html?rp=245912

In order to do what you want with SPFUN, you'll need to write your own
function (which can be an anonymous function):

S = sparse([1 0 0 0 0]);
result = spfun(@(x) x./0, S)

--
Steve Lord
slord@mathworks.com

Subject: sparse matrix multiplication: bad in Linux v7.5, good in Windows v7.1

From: Tim Davis

Date: 21 Jan, 2009 02:27:02

Message: 4 of 5

Or, you could you use "find" to extract the nonzeros, then do the scalar ./ or .* or whatever, and then rebuild the sparse matrix:

% note, not the best way to form a sparse matrix, but this is just a small example
% so it's OK:
A = sparse ([1 0 0 0 0]) ;

% now get the old behavior of ignoring the zeros in scalar A./s
s = 0 ;
[i j x] = find (A) ;
[m n] = size (A) ;
B = sparse (i, j, x./s, m, n) ;

Note the x./s, which operates on a dense matrix containing just the nonzeros of A,
which gives the result

B =

   (1,1) Inf

>> size (B)

ans =

     1 5

Subject: sparse matrix multiplication: bad in Linux v7.5, good in Windows v7.1

From: Akim

Date: 21 Jan, 2009 22:38:02

Message: 5 of 5

Thanks Tim and Steven, and Bruno for your help.

> % now get the old behavior of ignoring the zeros in scalar A./s
> s = 0 ;
> [i j x] = find (A) ;
> [m n] = size (A) ;
> B = sparse (i, j, x./s, m, n) ;

I think these code basically constitutes spfun, right?

In any case, it seems that given this bug fix, the simplest but somewhat crude solution is to divide by a very small, but non-zero number. E.g.

>> sparse([1 0 0 0 0])./1e-20
ans =
   (1,1) 1.0000e+20

For me this approximation is accurate enough.
Akim

Tags for 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