Binning/Sorting 3D data sets
Ältere Kommentare anzeigen
Hello all,
I have a data set with x, y, mass_of_particle (I have attached the data file). I wanted to sort the particle by creating new discretized x and y and accumulate the mass when it falls within the discretized positions as below,
A = dlmread('exp.txt','');
res = 400;
plane_size = 100;
st = -50;
en = 50;
B = zeros(res);
count = 0;
for x = 1:res
for y = 1:res
for i = 1:length(bbc)
if A(i,1)*1e3 >= st + ((res/plane_size) * (x-1)) ...
&& A(i,1)*1e3 < st + ((res/plane_size) * x)...
&& A(i,2)*1e3 >= st + ((res/plane_size) * (y-1))...
&& A(i,2)*1e3 < st + ((res/plane_size)*y)
B(x,y) = B(x,y) + A(i,3);
end
end
end
end
The above code should give something like this, meaning it should accumulate the particle mass that falls into the discretization,

The problem that I am facing is that the above code becomes computationally expensive if the number of particles increase and I am looking for something shorter as well, like using accumarray.
Thank you.
3 Kommentare
Ramesh Venkatasubramanian
am 27 Okt. 2020
The problem you're solving is actually more complicated than counting the number of particles in a 2D grid. What you're doing is adding all elements in column 3 of nx3 matrix A according to the binning of columns 1 and 2.
Your approach is to loop through each bin (400x400=160000 bins) and within each bin, loop through every row of matrix A which has 401150 rows. That's a total of 400*400*401150 which is more than 64 billion iterations of the conditional statements and sum().
See my answer which uses vectorization to avoid loops and cuts down the execution time by a factor of 845.
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Image Arithmetic finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
