# Thread: arrivals and departures in discrete time

1. ## arrivals and departures in discrete time

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. I haven't used Matlab, so I find the notation and examples not very accessible, but I think I see what's going on.

I am wondering though why you choose to write

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];

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

Also, the part I marked in red seems to be an inconsistency, and I would have expected this column to be omitted.

Am I off track?

Now I'm still not exactly sure what you're after, but consider that we can rewrite the arrays as arrays of ordered pairs, whereby

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

can be represented as

x_2=[[0 4] [3 4] [6 3]];

Now, perhaps we would be interested in these arrays:

s_2=[0 1 2 3 4 5 6];
a_2=[4 4 1 5 5 0 3];

where a_2 represents the number of items at a given time, and s_2 is superfluous because all the needed information is in the index of whichever element of a_2 we are dealing with.

If a_2 is of interest, it is pretty easy to get by iterating through x_2 and z_2 (comparable to x_2 in form) simultaneously (I would use a for() loop) and whenever x_2 has an entry whose time stamp matches the loop index, add to the current "running tally" and likewise subtract according to z_2 if applicable, until you reach the last index.

But if we really need s and a the way you wrote them, then I think the same approach would still work, it would just entail adding elements to the s-array and a-array as you go along, which is essentially the same task but with more bookkeeping.

Or possibly we would want to deal with

s_3=[0 2 3 5 6];
a_3=[4 1 5 0 3];

where now s_3 is no longer superfluous.

Hopefully what I wrote applies?

3. You haven't responded so I'm not sure if what I wrote in the first post applies, but another note:

If the data structure of arrays of ordered pairs is too much work to implement, it can be avoided.

I'll introduce the notation x[[1]] = the 1st element of x, just so that I can refer to it. I don't know what Matlab does.

Say we want a_2 as described above. Then we keep track of three indices, one for x, one for z, and one for a_2 (the loop index). Call the loop index i, call x's index j, and call z's index k. Whenever i equals x[[j]] or i equals z[[k]], we simply increment j or k until the value is no longer equal or until we reach the end of x or z, and update our tally accordingly.

4. Originally Posted by undefined
I haven't used Matlab, so I find the notation and examples not very accessible, but I think I see what's going on.

I am wondering though why you choose to write

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];

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

Also, the part I marked in red seems to be an inconsistency, and I would have expected this column to be omitted.

Am I off track?

Now I'm still not exactly sure what you're after, but consider that we can rewrite the arrays as arrays of ordered pairs, whereby

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

can be represented as

x_2=[[0 4] [3 4] [6 3]];

Now, perhaps we would be interested in these arrays:

s_2=[0 1 2 3 4 5 6];
a_2=[4 4 1 5 5 0 3];

where a_2 represents the number of items at a given time, and s_2 is superfluous because all the needed information is in the index of whichever element of a_2 we are dealing with.

If a_2 is of interest, it is pretty easy to get by iterating through x_2 and z_2 (comparable to x_2 in form) simultaneously (I would use a for() loop) and whenever x_2 has an entry whose time stamp matches the loop index, add to the current "running tally" and likewise subtract according to z_2 if applicable, until you reach the last index.

But if we really need s and a the way you wrote them, then I think the same approach would still work, it would just entail adding elements to the s-array and a-array as you go along, which is essentially the same task but with more bookkeeping.

Or possibly we would want to deal with

s_3=[0 2 3 5 6];
a_3=[4 1 5 0 3];

where now s_3 is no longer superfluous.

Hopefully what I wrote applies?
im not sure exactely what you mean with the consistency... perhaps there are other ways to write it, but im sure that at least graphically it is correct.
perhaps you are right though.... it probably requires some programming to restruct the vectors. i was hoping that it would be possible to solve with simple mathematical operations.... thank you anyway.. i will try this

5. Originally Posted by Lobotomy
im not sure exactely what you mean with the consistency... perhaps there are other ways to write it, but im sure that at least graphically it is correct.
perhaps you are right though.... it probably requires some programming to restruct the vectors. i was hoping that it would be possible to solve with simple mathematical operations.... thank you anyway.. i will try this
Well, s has 4 zeros corresponding to the 4 zeros in x; s has 3 sixes corresponding to the 3 sixes in x... wouldn't it then have 4 threes corresponding to the 4 threes in x? But you wrote s with 5 threes.

Anyway, I hope it works out.

6. Originally Posted by undefined
Well, s has 4 zeros corresponding to the 4 zeros in x; s has 3 sixes corresponding to the 3 sixes in x... wouldn't it then have 4 threes corresponding to the 4 threes in x? But you wrote s with 5 threes.

Anyway, I hope it works out.
ah now i get what you mean. well i have not found good relationship directly between s and a... s and a looks the way they do because of the other vectors...

the reason for s having 4 zeros is that x adds 4 items at time 0.
the reason for s having 1 "two" is that z subtracts 4 items at time 2 (4-3=1)
the reason for s having 5 "threes" is that x adds 4 items at time 3 (take the 1 from row above and add 4= 5)

etc etc....
next step till be 0 since 5-5 is 0. last step is when x adds 3items at time 6, hence 3 sixes.

well this is way much easier to explain graphically i dont know if you get my point