I have a 3D point cloud (XYZ) where the Z
can be position or energy. I want to project them on a 2D surface in a n-by-m grid (in my problem
The accumarray
function is quite suited for this kind of task. First I define example data:
table = [ 20*rand(1000,1) 30*rand(1000,1) 40*rand(1000,1)]; % random data
x_partition = 0:2:20; % partition of x axis
y_partition = 0:5:30; % partition of y axis
I'm assuming that
table
represent x, y, z respectivelyNaN
(if you want some other fill value, just change last argument of accumarray
).Then:
L = size(table,1);
M = length(x_partition);
N = length(y_partition);
[~, ii] = max(repmat(table(:,1),1,M) <= repmat(x_partition,L,1),[],2);
[~, jj] = max(repmat(table(:,2),1,N) <= repmat(y_partition,L,1),[],2);
ii = ii-1; % by assumption, all values in ii will be at least 2, so we subtract 1
jj = jj-1; % same for jj
result_maxdif = accumarray([ii jj], table(:,3), [M-1 N-1], @(v) max(v)-min(v), NaN);
result_sum = accumarray([ii jj], table(:,3), [M-1 N-1], @sum, NaN);
Notes to the code:
ii
and jj
, which give the indices of the x and y bins in which each point lies. I use repmat
to do that. It would have been better to usebsxfun
, but it doesn't support the multiple-output version of @max
.