Asked by Itzik Ben Shabat
on 14 May 2013

Hi, is there a function or an efficient way to find the overlap between two intervals represented in vectors. for example for a=[0 90 180 270]; and b=[26,180,270]; it should find the following overlaps and return the indices

26,90 (2,1) 90,180 (2,2) 180,270 (3,3) or (4,2) and the edges as well (as though the intervals are cyclic) 270,0 (1,4) 0,26 (1,1)

thanks

*No products are associated with this question.*

Answer by Cedric Wannaz
on 14 May 2013

Edited by Cedric Wannaz
on 14 May 2013

If boundaries are integer, a solution can built based on CUMSUM for building interval IDs and DIFF or UNIQUE for finding boundaries and IDs. I can develop this further if you are interested.

However, if all you have to do is to generate the list of intervals, without really caring for interval IDs, why couldn't you just go for something simpler, like:

>> bnd = unique([a, b]) ; >> if bnd(end) < 360, bnd = [bnd, bnd(1)] ; end >> int = [bnd(1:end-1); bnd(2:end)] int = 0 26 90 180 270 26 90 180 270 0

Answer by Thomas
on 14 May 2013

Your question is a little ambiguous but her is something you can use

doc intersect

To see where the arrays overlap

[C,ia,ib]=intersect(a,b)

Ans

C = 180.00 270.00 ia = 3.00 4.00 ib = 2.00 3.00

Itzik Ben Shabat
on 14 May 2013

Answer by Jan Simon
on 14 May 2013

What about `histc()`?

Itzik Ben Shabat
on 14 May 2013

Related Content

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn moreOpportunities for recent engineering grads.

Apply Today
## 1 Comment

## Jan Simon (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/75712#comment_148863

I'm not sure, what "26,90 (2,1) 90,180 (2,2)..." should mean. Of course this notation is clear to you, but an exact definition for the forum would be a good idea also.