MATLAB: Simulating events of varying duration

insert valuesloopvector

Hi there, I'm trying to simulate events of varying duration. I start with a vector of onsets (in seconds from the start of the simulated session):
onset=[23 40 67 88] etc
I then generate a list of durations (in seconds – each event is a different duration):
duration=[3 9 2 6]
I can use these to make a vector of offsets, however, what I want to end up with is a vector of all the seconds in which an event is occurring:
all=23 24 25 40 41 42 43 44 45 46 47 48 67 68 88 89 90 91 92 93
I am trying to write a loop that will do this, however can only make it work if all the durations are the same (e.g., 3 seconds). I'm really stuck – any help would be greatly appreciated. Thanks so much, Rebecca

Best Answer

st = [23 40 67 88];
du = [3 9 2 6];
m = cumsum(du);
t = m - du + 1;
s = zeros(1,m(end));
s(t) = 1;
ii = cumsum(s);
out = (1:m(end)) - t(ii) + st(ii);