# Which matrix operation should I use.

The title is quite vague, but I don't see how to phrase it.

I'm new to MatLab and have very little experience with matrix calculation.

Suppose a matrix "a" :

a =

2     5
6    12
13    17


It represent some intervals of values.

Then a matrix "b" that represent wider intervals :

b =

1     8
9    20


I need to compare these two matrix so the result will have the same number of rows than a and as many columns as there are rows in b. In that particular case the result should be :

result =

4     0
3     4
0     5


explanation : the first row of result is [4, 0] because the first row of a have 2 4 values included in the interval of the first rows of b and 0 in the second. 2, 3, 4 and 5 (from a) all falls between 1 and 8 (from b).

The second row is [3, 4] because 6, 7 and 8 fall between 1 and 8 (3 numbers in the first intervals of b) while 9, 10, 11 and 12 fall between 9 and 20 (4 numbers in the second interval of b).

If the intervals are $[p,q]$ and $[r,s]$, then the size of the intersection is $\max(0,1 + \min(q,s) - \max(p,r))$. So the following code should work:
m = size(a,1);