0
$\begingroup$

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).

1 Answers 1

0

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); n = size(b,1); lower = max( repmat(a(:,1),1,n), repmat(b(:,1),1,m).' ); upper = min( repmat(a(:,2),1,n), repmat(b(:,2),1,m).' ); c = max( 0, 1 + upper - lower ); 
  • 0
    I don't _really_ understand, but that seems to work just fine. Awesome. Would be nice if you could explain further.2012-06-21