I am working on 2D simulation of particles moving in a plane. The particles interact with other particles in some interaction radius, r. Thus i have to calculate interparticle distances, which gets messy as number of particles gets large. (I need large number of particles for better statistics and less boundary effects.)
I am currently doing this:
x=rand(1,N); y=rand(1,N); velocity=2*pi*(rand(1,N)-0.5);
D = pdist([x' y'],'euclidean'); M=sqaureform(D);
[I,J]=find(0<M & M<r);
for i = 1:N
interacting_particles = I(J==i);
average_velocity(i) = atan2(mean(sin(velocity(interacting_particles))),mean(cos(velocity(interacting_particles))));
average_velocity(i) = velocity(i);
relevant_quantities is a well defined function.
I hope my problem is clear. Also, I have access to an HPC server, but I really wish to do something cheaper than calculating D array of size 4096 GB.