-
Notifications
You must be signed in to change notification settings - Fork 0
/
smoothGridN.m
31 lines (25 loc) · 878 Bytes
/
smoothGridN.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
%% Helper function to simplify gridding
% Michael Nickerson 2019
% [[xg, yg, ...], fg] = smoothGridN([x, y, ..., f], N)
% Turns scattered data of arbitrary dimension into a smoothed grid
% Uses smoothn(<z>, 1, 'robust') for smoothing
function [outC, outD] = smoothGridN(inCD, N)
% Remove duplicates
inCD = unique(inCD, 'rows');
% Initialize variables
d = size(inCD,2)-1;
idx = NaN(size(inCD(:,1:d))); outC = NaN(N,d);
% Bin each coordinate dimension
for i=1:d
[idx(:,i), edgeD] = discretize(inCD(:,i), N);
outC(:,i) = edge2center(edgeD); % Want centers
end
% Bin values
outD = accumarray(idx, inCD(:,d), repmat(N, [1 d]), @mean, NaN);
% Smooth
outD = smoothn(outD, 2, 'robust');
% Helper function
function x = edge2center(x)
x = diff(x)/2 + x(1:end-1);
end
end