randfixedsum - Sum of numbers?
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
0 commentaires
Réponse acceptée
Roger Stafford
le 28 Jan 2014
Modifié(e) : Roger Stafford
le 28 Jan 2014
I notice that in your second for-loop you write
e = sum(on)-21600;
even though y = randfixedsum(4321,1,7200,0,7200) is set up to obtain a sum of only 7200. You will never correct for this mismatch with your for-loop procedure. You will probably end up with more than two thousand seconds too much for a sum in 'on'. I think you meant to write
e = sum(on)-7200;
If that isn't the source of the error I suggest you find the sums of both the x and y vectors when they are first created to see if these are as requested, with a reasonable allowance for round-off errors of course. If that checks out all right, then check your 'e' values as you emerge from each for-loop to see if at each of the two points it is exactly zero. I suspect one of these will uncover the error you are experiencing. If not, let us know.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Solver Outputs and Iterative Display dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!