Thread Subject: Sparse and logical element-wise operation

Subject: Sparse and logical element-wise operation

From: Wolfgang Schwanghart

Date: 14 Aug, 2009 12:51:04

Message: 1 of 5

Dear all,

I have two sparse, logical matrices (S1 and S2) with same size. I want to have a new sparse matrix Sn that contains true values in S1 that are false in S2.

Currently I have this solution

Sn = (S1-S2) > 0;

This is quite fast. Still, I feel there should be a better alternative, since my solution creates - in the first step - a sparse matrix that has double format and which is less sparse than the input matrices.

Sn = S1 & ~S2

won't work since it creates a nearly full matrix from S2.

Anyone an idea? Am I missing something trivial right now?

Best regards, Wolfgang

Subject: Sparse and logical element-wise operation

From: Wolfgang Schwanghart

Date: 14 Aug, 2009 13:06:05

Message: 2 of 5

Ok, more efficient in memory seems to be

Sn = xor(S2,S2 & S1);

I guess that's the solution, is it?

Best regards,
Wolfgang








"Wolfgang Schwanghart" <schwanghartremove.this@googlemail.com> wrote in message <h63mjo$flf$1@fred.mathworks.com>...
> Dear all,
>
> I have two sparse, logical matrices (S1 and S2) with same size. I want to have a new sparse matrix Sn that contains true values in S1 that are false in S2.
>
> Currently I have this solution
>
> Sn = (S1-S2) > 0;
>
> This is quite fast. Still, I feel there should be a better alternative, since my solution creates - in the first step - a sparse matrix that has double format and which is less sparse than the input matrices.
>
> Sn = S1 & ~S2
>
> won't work since it creates a nearly full matrix from S2.
>
> Anyone an idea? Am I missing something trivial right now?
>
> Best regards, Wolfgang

Subject: Sparse and logical element-wise operation

From: Bruno Luong

Date: 14 Aug, 2009 13:20:21

Message: 3 of 5

What about

Sn = S1>S2;

% Bruno

Subject: Sparse and logical element-wise operation

From: Wolfgang Schwanghart

Date: 14 Aug, 2009 13:33:18

Message: 4 of 5

Thanks alot Bruno.
Thoroughly embarrassed,
Wolfgang




"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h63oal$fh8$1@fred.mathworks.com>...
> What about
>
> Sn = S1>S2;
>
> % Bruno

Subject: Sparse and logical element-wise operation

From: Wolfgang Schwanghart

Date: 14 Aug, 2009 13:49:20

Message: 5 of 5

But... it seems that Bruno's solution is a bit slower. I guess that applying > involves conversion of both matrices to double.

>> S1 = sprand(1000,1000,0.3)>0;
>> S2 = sprand(1000,1000,0.3)>0;

>> tic; Sn = xor(S2,S2 & S1); toc
Elapsed time is 0.005284 seconds.
>> tic; Sn = S2>S1; toc
Elapsed time is 0.011113 seconds.

and for different sparsity.
>> S1 = sprand(1000,1000,0.01)>0;
>> S2 = sprand(1000,1000,0.01)>0;
>> tic; Sn = xor(S2,S2 & S1); toc
Elapsed time is 0.000482 seconds.
>> tic; Sn = S2>S1; toc
Elapsed time is 0.000684 seconds.





"Wolfgang Schwanghart" <schwanghartremove.this@googlemail.com> wrote in message <h63p2u$ekf$1@fred.mathworks.com>...
> Thanks alot Bruno.
> Thoroughly embarrassed,
> Wolfgang
>
>
>
>
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <h63oal$fh8$1@fred.mathworks.com>...
> > What about
> >
> > Sn = S1>S2;
> >
> > % 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
logical Wolfgang Schwanghart 14 Aug, 2009 08:54:09
sparse Wolfgang Schwanghart 14 Aug, 2009 08:54:08
rssFeed for this Thread

Contact us at files@mathworks.com