How to sum points in an 2-D area efficiently?
Afficher commentaires plus anciens
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
Plus de réponses (0)
Catégories
En savoir plus sur Hierarchical Clustering dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!