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

instead of, say,

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?