2020-11-27

Speed Up Finding Number of Elements Between Values Matlab

I've created what is a fairly simple MATLAB script to simulate the behaviour discussed in this question over on Maths SE.

clearvars;
samples = 1000;
x = 256;
r=exprnd(1/20e6,1,samples); % Generate exponentially distributed randoms.
endTime = sum(r);
quickMean=sum(r(1:x))/x; % Quick calc the mean and median.
quickMedian=0.693 * quickMean;
p = cumsum(r); % Convert event deltas into timestamps
bitstream = false(1,samples);
time = 0;
lastTime = 0;
for i = 1:samples
    lastTime = time;
    time = time + quickMedian;
    if (numel(p(p < time & p > lastTime)) > 0)
        bitstream(i) = true;
    end
    if (time > p(end))
        break
    end
end
ratio = sum(bitstream)/samples;

The script seems to work, however, if I use a large number of samples (say a million), which would be beneficial, it really crawls.

I'm assuming that the problematic statement is this one:

p(p < time & p > lastTime)

Is there a more efficient way to check if any elements in an array fall between two values?



from Recent Questions - Stack Overflow https://ift.tt/3o0OWSy
https://ift.tt/eA8V8J

No comments:

Post a Comment