I have two very large one dimensional arrays, 'aRef' which is around 11,000,000 elements and 'aTest' which is around 10,000,000 elements. I need to find the index of the closest element in 'aRef' for all elements in 'aTest'. 'aRef' is sorted and 'aTest' can be sorted if that will help performance.
– Method 1: Returns at out of memory error as the arrays are far too large
diff = abs(bsxfun(@minus,aRef,aTest'));[~, I] = min(diff);
– Method 2: Takes around 0.03 seconds per iteration (but varies greatly) and therefore around 300000 seconds in total
for k = 1:n diff = abs(aRef- aTest(k)); [~, I(k)] = min(diff);end
– Method 3: Takes around 0.013 seconds per iteration and therefore 130000 seconds in total
for k = 1:n i_lower = find(aRef <= aTest(k),1,'last'); i_higher = find(aRef >= aTest(k),1,'first'); end
Is there a more efficient method for this that won't exhaust the memory or take so long to run?
Thanks for your help.
Best Answer