randfixedsum - Sum of numbers?
Show older comments
Hi everyone, I've been using randfixedsum to simulate events occurring with a specified total time occurring across the simulated 8-hr day (28800 seconds) with specified bout range (e.g., 1 to 10 seconds) and number of events. It has been working brilliantly, but I can't get it to work for events that occur for a large proportion of the day (i.e., the total time the behaviour occurs for falls short of what it should). The assumptions of randfixedsum are not being violated (as far as I can see) and the code runs just fine, but the output isn't correct. I'm betting the answer is obvious, however I can't see it as I've spent a long time fiddling with different numbers! Any help would be greatly appreciated.
For "behaviour" occurring for 75% of total time (total time = 28800), bout range 1 to 10 seconds, 21600s of behaviour in total, 4320 events:
x = randfixedsum(4320,1,21600,1,10);
tp = .5; tm = -.5;
for k = 1:53
t0 = (tp+tm)/2;
du = round(x+t0); % Round with offset
e = sum(du)-21600;
if e > 0, tp = t0;
elseif e < 0, tm = t0;
else break % Break out when sum is correct
end
end
y = randfixedsum(4321,1,7200,0,7200);
tp = .5; tm = -.5;
for k = 1:53
t0 = (tp+tm)/2;
on = round(y+t0); % Round with offset
e = sum(on)-21600;
if e > 0, tp = t0;
elseif e < 0, tm = t0;
else break % Break out when sum is correct
end
end
h=[0]
dura=[h;du]'
b = cumsum(on)'
f=cumsum(dura)
onset=b+f
st=onset(1:4320)
duration=dura(2:4321)
m=cumsum(duration)
t = m - duration + 1;
s = zeros(1,m(end));
s(t) = 1;
ii = cumsum(s);
out = (1:m(end)) - t(ii) + st(ii);
sort(out)'
out=unique(out)
Thank you, Rebecca
Accepted Answer
More Answers (0)
Categories
Find more on MATLAB Compiler in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!