How to sum points in an 2-D area efficiently?
    조회 수: 4 (최근 30일)
  
       이전 댓글 표시
    
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);
댓글 수: 0
채택된 답변
  Guillaume
      
      
 2017년 1월 21일
        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
추가 답변 (0개)
참고 항목
카테고리
				Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
