# Arrivals and departures in discrete time

1. May 12, 2010

### Lobotomy

Hello
I have a problem.
im looking at discrete event arrivals of items.

the x-array represents the time of arrival and if one number occurs more than once it obviousely means that more than one item arrives at the exact same time. for instance:

x=[0 0 0 0 3 3 3 3 6 6 6];

this means that 4 items arrives at time 0, 4 more items arrive at time 3 and 3 more items arrive at time 6.

the y-array equals the size of the x-array and represents the accumulated number of items.
y=[1:11];

plotting this for instance in Matlab will show you this graphically:
plot(x,y,'*')

Now the thing is that i have yet another series of measurements, of items leaving. z represents the time they are leaving similar to x. w is the accumulated number of items that has left.

z=[2 2 2 5 5 5 5 5]
w=[1:8]

plotting these together:

hold
plot(z,w,['+','g'])

now what i want to do is the calculate the the amount of items in the system at every discrete time represented in the above vectors, hence the arriving items minus the leaving items. Sounds pretty simple but i cant figure out how to do it! The hard part is because the time scale is different...

so x,y and z,w are given to me beforehand. in reality these are vectors of lenght 10000, and there might be several arrivals in a row followed by several items leaving in a row, so this is why i can not do it by hand here. But in my small example, the result im looking for is:

s=[0 0 0 0 2 3 3 3 3 3 5 6 6 6];
a=[1 2 3 4 1 1 2 3 4 5 0 1 2 3];

plot(s,a,['<','r'])

notice that the a-vector must be modified since values are no longer added but subtracted.

the s-vector contains all the times of the x and z vectors, but the amount of items at each time is modified by hand.

My question is, is there a way to calculate s and a from x,y and z,w?? in the above example ive calculated it just by figuring out the result.... this seems pretty simple but well i just cant figure out how to do it. please help me someone

2. May 12, 2010

### EnumaElish

I don't know Matlab, but here's my take.

1. Take the union of x and -z: t=[0 0 0 0 -2 -2 -2 3 3 3 3 3 -5 -5 -5 -5 -5 6 6 6]
2. Construct a running sum of the elements in t: b= [1 2 3 4 3 2 1 2 3 4 5 6 5 4 3 2 1 2 3 4], by adding 1 for every non-negative element and subtracting 1 for every negative element in t.
3. Reduce t to s by:
3.a. deleting all except the last one of a sequence of identical negative elements, e.g. -2 -2 -2 (delete the first two 2's, keep the last one)
3.b. multiplying each negative element by -1
4. Reduce b to a by deleting an element if it is less than the previous one, unless the next one is higher. E.g., in "4 3 2 1 2" mark 3 for deletion because 4 > 3 and 3 > 2, mark 2 for deletion because 3 > 2 and 2 > 1, keep 1 because 1 < 2, keep 2 because 1 < 2.

3. May 12, 2010

### Lobotomy

hm ok... thanks ill try, but remember i cant do anywhing "by hand" all this has to be mathematical operations on a vector. since i cant go through the entire vectors manually since they are 10000 values long

4. May 13, 2010

### EnumaElish

Right; you should be able to come up with a simple algorithm for each individual step in my post.

5. May 13, 2010

### Lobotomy

yes it seems some simple programming is required rather than vector operations