Thread Subject: Prod of non zero element in a sparse matrix

Subject: Prod of non zero element in a sparse matrix

From: Nihal

Date: 9 Aug, 2011 16:10:31

Message: 1 of 6

Hi,
I have a sparse matrix A=[1 0 0 ; -1 0 1;1 -1 0] and I want to get the product of the non zeros element i.e. [-1 -1 1].
I have try to replace all zeros by 1 and then get the product:
A(A==0)=1
B=prod(A)

It does the job but for a 1000X2000 sparse matrix replacing all zeros by ones takes too much time. Any suggestions?

Thanks

Subject: Prod of non zero element in a sparse matrix

From: Matt J

Date: 9 Aug, 2011 16:25:31

Message: 2 of 6

"Nihal" wrote in message <j1rm5n$lrj$1@newscl01ah.mathworks.com>...
>
> It does the job but for a 1000X2000 sparse matrix replacing all zeros by ones takes too much time. Any suggestions?
==================


Something like this maybe:

real(exp(sum(spfun(@log,A))))

Subject: Prod of non zero element in a sparse matrix

From: Steven_Lord

Date: 9 Aug, 2011 16:42:54

Message: 3 of 6



"Nihal " <nihalsameh@yahoo.com> wrote in message
news:j1rm5n$lrj$1@newscl01ah.mathworks.com...
> Hi,
> I have a sparse matrix A=[1 0 0 ; -1 0 1;1 -1 0] and I want to get the
> product of the non zeros element i.e. [-1 -1 1].
> I have try to replace all zeros by 1 and then get the product:
> A(A==0)=1
> B=prod(A)
>
> It does the job but for a 1000X2000 sparse matrix replacing all zeros by
> ones takes too much time. Any suggestions?

A = sparse([1 2 3 3 2], [1 1 1 2 3], [1, -1, 1, -1, 1])
prod(nonzeros(A))

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Prod of non zero element in a sparse matrix

From: Matt J

Date: 9 Aug, 2011 17:50:25

Message: 4 of 6

"Steven_Lord" <slord@mathworks.com> wrote in message <j1ro2e$rod$1@newscl01ah.mathworks.com>...
>
> > It does the job but for a 1000X2000 sparse matrix replacing all zeros by
> > ones takes too much time. Any suggestions?
>
> A = sparse([1 2 3 3 2], [1 1 1 2 3], [1, -1, 1, -1, 1])
> prod(nonzeros(A))
================

Don't think so. The OP asked for the _columnwise_ product of non-zero matrix values.

Subject: Prod of non zero element in a sparse matrix

From: Steven_Lord

Date: 9 Aug, 2011 18:11:51

Message: 5 of 6



"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message
news:j1rs10$ca6$1@newscl01ah.mathworks.com...
> "Steven_Lord" <slord@mathworks.com> wrote in message
> <j1ro2e$rod$1@newscl01ah.mathworks.com>...
>>
>> > It does the job but for a 1000X2000 sparse matrix replacing all zeros
>> > by ones takes too much time. Any suggestions?
>>
>> A = sparse([1 2 3 3 2], [1 1 1 2 3], [1, -1, 1, -1, 1])
>> prod(nonzeros(A))
> ================
>
> Don't think so. The OP asked for the _columnwise_ product of non-zero
> matrix values.

You're right; I skimmed the OP's prose and based my response off that. My
mistake.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Prod of non zero element in a sparse matrix

From: Bruno Luong

Date: 9 Aug, 2011 19:31:27

Message: 6 of 6

"Nihal" wrote in message <j1rm5n$lrj$1@newscl01ah.mathworks.com>...
> Hi,
> I have a sparse matrix A=[1 0 0 ; -1 0 1;1 -1 0] and I want to get the product of the non zeros element i.e. [-1 -1 1].
> I have try to replace all zeros by 1 and then get the product:
> A(A==0)=1
> B=prod(A)
>
> It does the job but for a 1000X2000 sparse matrix replacing all zeros by ones takes too much time. Any suggestions?
>
> Thanks

 [i j s] = find(A);
B = accumarray(j,s,[size(A,1) 1], @prod)

% Bruno

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
prod Nihal 9 Aug, 2011 12:14:12
sparse Nihal 9 Aug, 2011 12:14:12
rssFeed for this Thread

Contact us at files@mathworks.com