Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a sequence from vectors of start and end numbers

How is it possible to create a sequence if I have vectors of starting and ending numbers of the subsequences in a vectorized way in Matlab?

Example Input:

A=[12 20 34]  
B=[18 25 37]

I want to get (spacing for clarity):

C=[12 13 14 15 16 17 18    20 21 22 23 24 25    34 35 36 37]
like image 322
user2129506 Avatar asked Dec 05 '22 03:12

user2129506


1 Answers

Assuming that A and B are sorted ascending and that B(i) < A(i+1) holds then:

idx = zeros(1,max(B)+1);
idx(A) = 1;
idx(B+1) = -1;

C = find(cumsum(idx))

To get around the issue mentioned by Dennis in the comments:

m = min(A)-1;
A = A-m;
B = B-m;

idx = zeros(1,max(B)+1);
idx(A) = 1;
idx(B+1) = -1;

C = find(cumsum(idx)) + m;
like image 153
Dan Avatar answered Dec 29 '22 09:12

Dan