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:
taking the exponential of only the nonzero elements of a sparse matrix

Subject: taking the exponential of only the nonzero elements of a sparse matrix

From: Akim

Date: 15 Jan, 2009 06:39:01

Message: 1 of 10

Dear All,

Matlab help tells me that "Multiplication and division are performed on only the nonzero elements of sparse matrices." What about something similar for the exponential?

Specifically, given a sparse matrix A, I wish to obtain a sparse matrix B, such that

B(i,j)=exp(A(i,j)), for all nonzero A(i,j)
B(i,j)=0, for all zero A(i,j)

I'm only interested in the exponents of the nonzeros, and calculating the exponents of a vast number of zeros slows things down a little bit.

Thank you for your help.

Subject: taking the exponential of only the nonzero elements of a sparse matrix

From: nor ki

Date: 15 Jan, 2009 12:59:02

Message: 2 of 10

"Akim " <aaa@bbb.ccc> wrote in message <gkmlm5$f7s$1@fred.mathworks.com>...
> Dear All,
>
> Matlab help tells me that "Multiplication and division are performed on only the nonzero elements of sparse matrices." What about something similar for the exponential?
>
> Specifically, given a sparse matrix A, I wish to obtain a sparse matrix B, such that
>
> B(i,j)=exp(A(i,j)), for all nonzero A(i,j)
> B(i,j)=0, for all zero A(i,j)
>
> I'm only interested in the exponents of the nonzeros, and calculating the exponents of a vast number of zeros slows things down a little bit.
>
> Thank you for your help.

Hi Akim,

why not try

B = exp(A(A~=0))

hth kinor

Subject: taking the exponential of only the nonzero elements of a sparse matrix

From: Bruno Luong

Date: 15 Jan, 2009 13:16:01

Message: 3 of 10


>
> B(i,j)=exp(A(i,j)), for all nonzero A(i,j)
> B(i,j)=0, for all zero A(i,j)
>

[i j s]=find(A);
B=sparse(i,j,exp(s));

Bruno

Subject: taking the exponential of only the nonzero elements of a sparse matrix

From: Bruno Luong

Date: 15 Jan, 2009 13:19:02

Message: 4 of 10

>
> why not try
>
> B = exp(A(A~=0))
>

Probably because B does not have the right shape.

Bruno

Subject: taking the exponential of only the nonzero elements of a sparse matrix

From: nor ki

Date: 15 Jan, 2009 16:05:23

Message: 5 of 10

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <gknd46$gkt$1@fred.mathworks.com>...
> >
> > why not try
> >
> > B = exp(A(A~=0))
> >
>
> Probably because B does not have the right shape.
>
> Bruno

Hi Bruno,

hmm sad but true.. thank you

i did not check because

k = zeros(3)
k(1) = 1
kk = sparse(k)
res = exp(kk(kk~=0))

gives and looked nice to me..

kk = (1,1) 1
res = (1,1) 2.7183

but

full(kk) is ok unlike
full(res) gives 2.7183

cheers
kinor

Subject: taking the exponential of only the nonzero elements of a sparse matrix

From: Matt

Date: 15 Jan, 2009 16:36:02

Message: 6 of 10

"Akim " <aaa@bbb.ccc> wrote in message <gkmlm5$f7s$1@fred.mathworks.com>...
> Dear All,
>
> Matlab help tells me that "Multiplication and division are performed on only the nonzero elements of sparse matrices." What about something similar for the exponential?
>
> Specifically, given a sparse matrix A, I wish to obtain a sparse matrix B, such that
>
> B(i,j)=exp(A(i,j)), for all nonzero A(i,j)
> B(i,j)=0, for all zero A(i,j)
>
> I'm only interested in the exponents of the nonzeros, and calculating the exponents of a vast number of zeros slows things down a little bit.
>
> Thank you for your help.


B = spfun(@exp,A);

Subject: taking the exponential of only the nonzero elements of a sparse matrix

From: Roger Stafford

Date: 15 Jan, 2009 17:37:02

Message: 7 of 10

"Akim " <aaa@bbb.ccc> wrote in message <gkmlm5$f7s$1@fred.mathworks.com>...
> Dear All,
>
> Matlab help tells me that "Multiplication and division are performed on only the nonzero elements of sparse matrices." What about something similar for the exponential?
>
> Specifically, given a sparse matrix A, I wish to obtain a sparse matrix B, such that
>
> B(i,j)=exp(A(i,j)), for all nonzero A(i,j)
> B(i,j)=0, for all zero A(i,j)
>
> I'm only interested in the exponents of the nonzeros, and calculating the exponents of a vast number of zeros slows things down a little bit.
>
> Thank you for your help.

  Akim, in doing what you ask there is the risk of misinterpreting your results in B. In those cases where A contained a legitimate zero for which the answer ought to be one, you would have zeros instead. In those cases where A was minus infinity (-inf) the answer would also be zero. There is no easy way afterwards of distinguishing between the two possibilities in B.

  I would think you would want all the zeros in A replaced by ones, which could be done rather efficiently without using 'exp' - either that or produce a B of smaller size with all the zeros eliminated as Nor Ki has done. Of course the cost of the former is the loss of being sparse. The notion of a sparse exponential result seems to me inherently self-contradictory unless we extend the notion of sparsity to encompass constants other than zero.

Roger Stafford

Subject: taking the exponential of only the nonzero elements of a sparse matrix

From: Akim

Date: 16 Jan, 2009 03:57:02

Message: 8 of 10

Dear Kinor, Bruno, Matt, Roger -- thank you all very much for your help.

spfun is great! -- and actually very similar to Bruno's suggestion:

%begin spfun
[i,j,x] = find(s);
[m,n] = size(s);
f = sparse(i,j,feval(fun,x),m,n);
%end spfun

In my simulations spfun runs slightly faster cf. Bruno's code, apparently because feval(@exp,x) is faster than exp(x).

Re: Roger's comment
> Akim, in doing what you ask there is the risk of misinterpreting your results in B. In those cases where A contained a legitimate zero for which the answer ought to be one, you would have zeros instead. In those cases where A was minus infinity (-inf) the answer would also be zero. There is no easy way afterwards of distinguishing between the two possibilities in B.

Good point. In my problem all the nonzeros are positive, so distinguishing between 0 and -inf is not a problem.

Best Regards,
Akim

Subject: taking the exponential of only the nonzero elements of a sparse matrix

From: Matt

Date: 16 Jan, 2009 09:46:02

Message: 9 of 10

"Akim " <aaa@bbb.ccc> wrote in message <gkp0id$666$1@fred.mathworks.com>...
>
> In my simulations spfun runs slightly faster cf. Bruno's code, apparently because feval(@exp,x) is faster than exp(x).

I don't think so.

I'm pretty sure the reason is that spfun(A) has to generate new matrix elements only, but not a new table of indices of the explicit matrix elements. The index table is shared with the original matrix A using a copy-on-write pointer.

When you call sparse() however, MATLAB does no data sharing and creates an entirely new sparse matrix object from scratch.

Subject: taking the exponential of only the nonzero elements of a sparse matrix

From: Matt

Date: 16 Jan, 2009 17:03:02

Message: 10 of 10

"Matt " <mjacobson.removethis@xorantech.com> wrote in message <gkpl0q$7dj$1@fred.mathworks.com>...
> "Akim " <aaa@bbb.ccc> wrote in message <gkp0id$666$1@fred.mathworks.com>...
> >
> > In my simulations spfun runs slightly faster cf. Bruno's code, apparently because feval(@exp,x) is faster than exp(x).
>
> I don't think so.
>
> I'm pretty sure the reason is that spfun(A) has to generate new matrix elements only, but not a new table of indices of the explicit matrix elements. The index table is shared with the original matrix A using a copy-on-write pointer.
>
> When you call sparse() however, MATLAB does no data sharing and creates an entirely new sparse matrix object from scratch.
>
>


Actually, I've done some of my own tests and don't see a significant speed difference between either approach. Of course, I don't know what matrix size and density you are using...

Anyway, my above conjecture might be no more than that.

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