Remarks:
- all this can be almost one-liner via python pandas and cutting methods.
- I've rewritten your random cloud initialization
What you can do is
- layout an xy grid via
meshgrid
, - project the cloud on xy (simple marginalization)
- find the nearest grid point via a
kd-tree
search, i.e. label your data associating to each cloud point a grid node - group data by label and evaluate your local statistic (via
accumarray
).
Here's a working example:
samples = 500;
%data extrema
xl = 0; xr = 1; yl = 0; yr = 1;
% # grid points
sz = 20;
% # new random cloud
table = [random('Uniform',xl,xr,[samples,1]) , random('Uniform',yr,yl,[samples,1]), random('normal',1,1,[samples,1])];
figure; scatter3(table(:,1),table(:,2),table(:,3));
% # grid construction
xx = linspace(xl,xr,sz); yy = linspace(yl,yr,sz);
[X,Y] = meshgrid(xx,yy);
grid_centers = [X(:),Y(:)];
x = table(:,1); y = table(:,2);
% # kd-tree
kdtreeobj = KDTreeSearcher(grid_centers);
clss = kdtreeobj.knnsearch([x,y]); % # classification
% # defintion of local statistic
local_stat = @(x)sum(x) % # for total energy
% local_stat = @(x)max(x)-min(x) % # for position off-set
% # data_grouping
class_stat = accumarray(clss,table(:,3),[],local_stat );
class_stat_M = reshape(class_stat , size(X)); % # 2D reshaping
figure; contourf(xx,yy,class_stat_M,20);