# locate particles in squares (combinatorics)

1 Ansicht (letzte 30 Tage)
Mikhail am 16 Okt. 2014
Kommentiert: Mikhail am 16 Okt. 2014
I have a problem: I have a lot of particles with given coordinates (x,y). My (x,y) plane is divided into a bunch of squares 32x32. I need a fast way to identify, which particles belongs to which square.
Input data: data(Nx3)=(x1,y1,id1;x2,y2,id2,....) - array with particles coordinates and id's. Also could have array with id's of squares, like: (x1,y1,x2,y2,id#; ...)
What I want on output is array out(Nx2) containing id of particle and corresponding square id.
I don't need you to write a code for me. I want just idea -how to do it in the most fast and convinient way. Thanks
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Guillaume am 16 Okt. 2014
If you divide the x and y coordinates of your particles by your square size, you get in which column/row square your particles are in. Then just convert that into linear indexing with sub2ind (assuming you use the same column-major ordering as matlab). You can do the calculation on all particles at once, something like:
colsquare = floor(data(:, 1) / 32) + 1;
rowsquare = floor(data(:, 2) / 32) + 1;
squareidx = sub2ind([numsquarey numsquarex], rowsquare, colsquare);
out = [data(:, 3) squareidx];
##### 1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Mikhail am 16 Okt. 2014
wow, that appeared to be easy... thanks

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Discrete Math finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by