Make a grid (1 x1) out of scattered insitu data and take the average of each grid that has data

1 view (last 30 days)
CHARLES ADDEY
CHARLES ADDEY on 20 Jan 2022
Commented: Mathieu NOE on 21 Jan 2022
Hello,
Please, I am working on some insitu scattered dataset, I want to make the data into a grid, and then average all the data within that grid, such as the image below were I have latitude, longitude and a third variable (temperature). I want to grid it into 1 x 1 resolution.
Latitude = 22 - 25
Longitude = 113 - 118
Please what is the best way to do this? Thanks in advance.

Accepted Answer

Mathieu NOE
Mathieu NOE on 20 Jan 2022
hello
this would be my suggestion, based on dummy data (left picture) - result appears in right picture
clc
clearvars
% dummy data
n = 50;
x = linspace(112,120,n)+0.25*rand(1,n); % Longitude
y = linspace(22,24,n)+0.5*rand(1,n); % Latitude
z = 10+y; % temperature
figure(1),
scatter(x,y,z,z,'filled');
colormap('jet');
colorbar('vert');
%% main code
% grid spacing and min / max values for display
dx = 1;
dy = 1;
xmin = 113;
xmax = 118;
ymin = 22;
ymax = 25;
x2 = xmin:dx:xmax;
y2 = ymin:dy:ymax;
xc = x2(1:end-1)+0.5*dx;
yc = y2(1:end-1)+0.5*dy;
figure(2),hold on
for ci = 1:numel(x2)-1
ind1 = find((x>=x2(ci)) & (x<=x2(ci+1)));
y_selected = y(ind1);
z_selected = z(ind1);
for ck = 1:numel(y2)-1
ind2 = find((y_selected>=y2(ck)) & (y_selected<=y2(ck+1)));
z_mean(ck,ci) = mean(z_selected(ind2));
scatter(xc(ci),yc(ck),150,z_mean(ck,ci),'filled');
end
end
colormap('jet');colorbar('vert');
xlim([xmin xmax]);
ylim([ymin ymax]);
xticks(x2);
yticks(y2);
view(2);
hold off

More Answers (1)

CHARLES ADDEY
CHARLES ADDEY on 21 Jan 2022
Thanks alot @Mathieu NOE, this really does what i need

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!