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