Effacer les filtres
Effacer les filtres

How to sum points in an 2-D area efficiently?

1 vue (au cours des 30 derniers jours)
Ahmad Moniri
Ahmad Moniri le 21 Jan 2017
Commenté : Ahmad Moniri le 21 Jan 2017
I have an NxN array which is modelling an array of sensors and I have many objects that lie on this array. Each object belongs to a cluster. The objects may be anywhere on the array (e.g. (1.212, 4.567) ) but the array has a spatial accuracy that is unity. So all the objects in each individual array sensor needs to be accumulated. The below image helps visualise my problem.
Currently I have each cluster in a cell. And each cell contains two vectors with the x and y co-ordinates of the objects that belong to that cluster. The function below is a solution to my problem but it is very inefficient and is not practical. I was wondering if there are more efficient solutions and whether I am being naive. I am open to changing the structure of the my variables but each cluster has a different number of objects so I decided cells were appropriate.
function [ Z ] = sumSensor( clusters, num_of_clusters, N )
% Inputs: clusters is a struct with the vectors clusters.myobj.x and clusters.myobj.y representing the co-ordinates of the objects on the array
Z = zeros(N,N);
% For every sensor...
for i = 1:N
for j = 1:N
% And every cluster...
for k = 1:num_of_clusters
% Search for every object on the sensor and accumulate them
Z(i,j) = Z(i,j) + sum((floor(clusters{k}.myobj.x)==i) & (floor(clusters{k}.myobj.y)==j));
end
end
end
end
An test case could be:
N = 32;
num_of_clusters = 5;
clusters = cell(num_of_clusters,1);
for i = 1:num_of_clusters
clusters{i} = struct();
% Random integer between 10000 and 110000
randLength = round(10E4+10E5*rand);
% Random co-ordinates between 1 and N+1
clusters{i}.myobj.x = 1 + N*rand(1,randLength);
clusters{i}.myobj.y = 1 + N*rand(1,randLength);
end
Z = sumSensor(clusters, 2, N);

Réponse acceptée

Guillaume
Guillaume le 21 Jan 2017
Your intermediate field myobj that does not appear to serve any purpose slightly complicates things. I would avoid multilevel structures if there's no need to. Similarly the cell array adds another level of indirection that's probably not needed.
Anyway,
function Z = sumSensor(clusters) %neither num_of_clusters nor N are needed
clusters = [clusters{:}]; %transform the cell array of scalar structures into a structure array.
clusters = [clusters.myobj]; %get rid of the intermediate field.
x = [clusters.x]; %put all the x together since you don't care which cluster they belong to for this function
y = [clusters.y]; %same with y
Z = accumarray(floor([x', y']), 1);
end
  1 commentaire
Ahmad Moniri
Ahmad Moniri le 21 Jan 2017
Thank you so much Guillaume. Your answer makes complete sense and is very elegant and efficient.
The only issue was in the special case that the array was empty (or incomplete). To solve this I placed a single object in every sensor and then removed a matrix of ones from the output.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by