Thread Subject:
speed up "intersect" for 2 vectors

Subject: speed up "intersect" for 2 vectors

From: Mikael

Date: 5 Jul, 2012 15:56:23

Message: 1 of 7

Hello,
I have an time-efficiency issue using intersect:
[~,~,ib] = intersect(A, B);

A and B are vectors.
A is a row-vector ~1x60
B a column-vector ~300x1

This line is called thousands of times.
Do you have an idea how to speed it up ?

Thanks,

Mikaƫl

Subject: speed up "intersect" for 2 vectors

From: Matt J

Date: 5 Jul, 2012 18:54:06

Message: 2 of 7

"Mikael" wrote in message <jt4df7$hm9$1@newscl01ah.mathworks.com>...
> Hello,
> I have an time-efficiency issue using intersect:
> [~,~,ib] = intersect(A, B);
>
> A and B are vectors.
> A is a row-vector ~1x60
> B a column-vector ~300x1
>
> This line is called thousands of times.
> Do you have an idea how to speed it up ?
=================

How is this a time-efficiency issue? On my old dual core 32-bit laptop, I can execute the above 1000s of times in just a few seconds.

Subject: speed up "intersect" for 2 vectors

From: Mikael

Date: 6 Jul, 2012 07:09:08

Message: 3 of 7

"Matt J" wrote in message <jt4nse$ma$1@newscl01ah.mathworks.com>...
> "Mikael" wrote in message <jt4df7$hm9$1@newscl01ah.mathworks.com>...
> > Hello,
> > I have an time-efficiency issue using intersect:
> > [~,~,ib] = intersect(A, B);
> >
> > A and B are vectors.
> > A is a row-vector ~1x60
> > B a column-vector ~300x1
> >
> > This line is called thousands of times.
> > Do you have an idea how to speed it up ?
> =================
>
> How is this a time-efficiency issue? On my old dual core 32-bit laptop, I can execute the above 1000s of times in just a few seconds.

Yes, I should have said hundreds of thousands times!

I have realised this morning that some details could help:
A = a1:a2, with a1 =< a2 integers
B is sorted.

So, as a first approach:
[~,i1] = ismember(a1,B);
[~,i2] = ismember(a2,B);
ib = B(i1:i2);

Now this form is not faster but hopefully in a few minutes, I will find something efficient!

Subject: speed up "intersect" for 2 vectors

From: Mikael

Date: 6 Jul, 2012 07:23:07

Message: 4 of 7

> A = a1:a2, with a1 =< a2 integers
> B is sorted.
>
> So, as a first approach:
> [~,i1] = ismember(a1,B);
> [~,i2] = ismember(a2,B);
> ib = B(i1:i2);

This line is 30% mor efficient than the former one:
ib = B(find(a1 == B):find(a2 == B);

I am trying to find a method with 'any'.

Subject: speed up "intersect" for 2 vectors

From: Bruno Luong

Date: 6 Jul, 2012 07:31:12

Message: 5 of 7

"Mikael" wrote in message <jt62uk$spp$1@newscl01ah.mathworks.com>...

>
> So, as a first approach:
> [~,i1] = ismember(a1,B);
> [~,i2] = ismember(a2,B);
> ib = B(i1:i2);

To be consistent with your first post, ib should be i1:i2. Or alternatively

ib = find(B>=A(1),1,'first'):find(B<=A(end),1,'last')

% If they do intersect. Or

[~, loc] = histc(A([1 end]),B);
ib = loc(1):loc(2)

% Bruno

Subject: speed up "intersect" for 2 vectors

From: Bruno Luong

Date: 6 Jul, 2012 08:09:07

Message: 6 of 7

"Mikael" wrote in message <jt63or$27j$1@newscl01ah.mathworks.com>...
> > A = a1:a2, with a1 =< a2 integers
> > B is sorted.
> >
> > So, as a first approach:
> > [~,i1] = ismember(a1,B);
> > [~,i2] = ismember(a2,B);
> > ib = B(i1:i2);
>
> This line is 30% mor efficient than the former one:
> ib = B(find(a1 == B):find(a2 == B);
>
> I am trying to find a method with 'any'.

Careful if B is strictly in between a1 and a2.

Bruno

Subject: speed up "intersect" for 2 vectors

From: Mikael

Date: 6 Jul, 2012 09:12:07

Message: 7 of 7

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <jt66f3$avc$1@newscl01ah.mathworks.com>...
> "Mikael" wrote in message <jt63or$27j$1@newscl01ah.mathworks.com>...
> > > A = a1:a2, with a1 =< a2 integers
> > > B is sorted.
> > >
> > > So, as a first approach:
> > > [~,i1] = ismember(a1,B);
> > > [~,i2] = ismember(a2,B);
> > > ib = B(i1:i2);
> >
> > This line is 30% mor efficient than the former one:
> > ib = B(find(a1 == B):find(a2 == B);
> >
> > I am trying to find a method with 'any'.
>
> Careful if B is strictly in between a1 and a2.
>
> Bruno

Indeed!
Thank you your method works well. It would be difficult to be more efficient, I guess.

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
intersect Mikael 5 Jul, 2012 11:59:21
ismember Mikael 5 Jul, 2012 11:59:21
speed up Mikael 5 Jul, 2012 11:59:21
rssFeed for this Thread

Contact us