fastest way to find difference values, within a certain range, between each element in two large vectors. Too large for bsxfun alone

2 views (last 30 days)
current having to use two loops but it is not optimized for time. I want to do this in the fastest way possible because arrays can be quite large (12mil-6mil elements, in ascending order)
here is the code i have so far. the point is to create a histogram of differences from tmin to tmax (for example -100000 to 100000) with 1000 as the binsize. c is the counts in the histogram. HELP!!
function [c, dt, dtEdges] = coincidence4(time_a,time_b,tmin,tmax,binsize)
% round tmin, tmax to a intiger multiple of binsize:
if mod(tmin,binsize)~=0
tmin=tmin-mod(tmin,binsize)+binsize;
end
if mod(tmax,binsize)~=0
tmax=tmax-mod(tmax,binsize);
end
dt = tmin:binsize:tmax;
dtEdges = [dt(1)-binsize/2,dt+binsize/2];
c = zeros(1,length(dt));
Na = length(time_a);
Nb = length(time_b);
tic1=tic;
tic2=tic1;
bbMin=1;
for aa = 1:Na
ta = time_a(aa);
bb = bbMin;
while (bb<=Nb)
tb = time_b(bb);
d = tb - ta;
if d < tmin
bbMin = bb;
bb = bb+1;
elseif d > tmax
bb = Nb+1;
else
index = floor((d-dtEdges(1))/(dtEdges(end)-dtEdges(1))*(length(dtEdges)-1)+1);
c(index)=c(index)+1;
bb = bb+1;
end
end
end
toc(tic1)
end

Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!