MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
Asked by Rebecca on 4 Feb 2013

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

*No products are associated with this question.*

Answer by Andrei Bobrov on 7 Feb 2013

Accepted 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);

Answer by Brian B on 7 Feb 2013

This seems to be what you are looking for:

onset=[23 40 67 88] duration=[3 9 2 6] all=cell2mat(arrayfun(@(a,b)a+(1:b)-1,onset,duration,'UniformOutput',0))

Brian B on 7 Feb 2013

If there is the possibility of overlap between events, you may need to use

all = unique(all)

to sort and remove duplicate entries for instants when multiple events are occurring; of course that depends on your application.

Answer by Youssef KHMOU on 4 Feb 2013

An example :

events=zeros(1,1000); for (t=0:1000)

if mod(t,2)==0 events(t)=a; elseif mod(t,3)==0 events(t)=b; elseif mod(t,7)==0 events(t)=c; ..... end

with constants a,b,c, the onsets .

is that OK?

Show 2 older comments

Rebecca on 5 Feb 2013

Thanks again for your response. Just to clarify - the onsets are not evenly spaced apart (e.g., they could be 3s, then 25s, then 100s) - does this loop still work with this? Also, the duration (how many seconds each event occurs for - I think you've called this magnitude), is also not constant. Again, forgive my ignorance, I hope I've understood and really appreciate the help! Thanks,Rebecca

Youssef KHMOU on 5 Feb 2013

i need more details about your simulation, if you post the problem as it should be, it will be easy to solve it, how the events take place, randomly? give simple example from your code so as we can work on it,

Answer by Rebecca on 7 Feb 2013

Ok. I start with something like this to give me a list of random numbers (25 numbers as an example):

onset=randperm(3600,25)’

These numbers represent the onsets of a human behaviour (e.g., laughing). At the moment, I am randomly selecting the onsets. I then put them in order:

sort(onset)

Then, I generate a list of random duration times. For example, I specify that the duration of each instance of laughing will be either 4, 5, or 6 seconds.

duration=[4 5 6]

To generate my list of durations that will represent the durations of my onsets, I do this:

y=datasample(duration,25)'

To give you some sample output:

onset= 128 351 457 509 566 1001 1512 1742 1965 2274 2348 2428 2838 2870 2934 3039 3260 3282 3286 3341 3435 3436 3440 3465 3484 and duration = 6 6 5 5 4 6 4 4 4 4 6 6 4 6 4 5 5 6 6 4 5 5 5 6 6 So, the first behaviour starts at second 128 and goes for 6 seconds (i.e. the offset is 134). The second behaviour startes at 351 and goes for 6 seconds (offset is 357). The third behaviour starts at 457 and goes for 5 seconds(offset is 462)... What I want is a vector of all the seconds in which a behaviour is occurring...

128 129 130 131 132 133 351 352 353 354 355 356 457 458 459 460 461 etc

Does this make sense? Once again, thanks for your help, I hope I have explained it clearly! Rebecca

## 0 Comments