필터 지우기
필터 지우기

randfixedsum - Sum of numbers?

조회 수: 6 (최근 30일)
Rebecca
Rebecca 2014년 1월 28일
댓글: Rebecca 2014년 1월 28일
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

채택된 답변

Roger Stafford
Roger Stafford 2014년 1월 28일
편집: Roger Stafford 2014년 1월 28일
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.
  댓글 수: 1
Rebecca
Rebecca 2014년 1월 28일
That was it! Thanks very much, it was staring me in the face the whole time! :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by