Quantcast
Channel: Vlad's Blog » vvladd
Viewing all articles
Browse latest Browse all 10

Efficient computation of element presence in a set of windows

$
0
0

It seems trivial to verify presence of an element (x,y) in a rectangle window. Now imagine that you have millions of elements and many thousands or more of windows. Your problem quickly gets unwieldy as it scales as badly as O(N^2)!

I propose a simple solution based on buckets. First the whole space is split into small, say 5×5 regions or cells. Given a window and a set of elements, it is very easy to find the top-left and top right cell that belongs to the window. The cells that are inside are easy to find too. Next, given each element and knowing the width of each cell, it is very easy to calculate which cell in X or Y direction the element belongs to.

My implementation is in MATLAB MEX. There is only one for loop that puts every element into its cell. Then a for loop over windows efficiently finds all the elements falling into it by considering now cells, not the individual elements. The size of a cell can be tuned by the user.

The code as well as 64bit MATLAB binary for Mac can be downloaded from [HERE]. The code is licensed under BSD license.


Viewing all articles
Browse latest Browse all 10

Trending Articles