How to calculate the number of points in each rectangular grid cell
21 views (last 30 days)
Show older comments
Sagar Karmakar
on 9 Aug 2023
Commented: Star Strider
on 11 Aug 2023
I possess 10,000 data points corresponding to each value of x and y, and I intend to determine the count of (x, y) points falling within distinct grid cells. To achieve this, I partition the x-y plane into a meshgrid, subsequently tallying the occurrence of points within each cell. This process involves calculating the frequency of points present in each individual cell.
Below is the code I have formulated to carry out this procedure:
x_hit = transpose(x(end,:)); %x data
y_hit = transpose(y(end,:)); % y data
x_values = linspace(0,1 , 100);
y_values = linspace(5, 14, 100);
% Create a grid of x and y values
[x_grid, y_grid] = meshgrid(x_values, y_values);
cell_counts = zeros(length(x_values), length(y_values));
% Iterate through each trajectory
for i = 1:length(x_hit)
x_pos = x_hit(i);
y_pos = y_hit(i);
% Find the indices of the cell in the grid
x_cell = find(x_values <= x_pos, 1, 'last');
y_cell = find(y_values <= y_pos, 1, 'last');
% Increment the cell count
cell_counts(x_cell, y_cell) = cell_counts(x_cell, y_cell) + 1;
end
The purpose of "cell_counts" is to calculate the quantity of points within each cell. However, I am uncertain whether the code is functioning correctly for my specific issue, as the anticipated outcome has not been achieved. Could someone please assess whether the aforementioned code is suitable for my task? Alternatively, is there an alternative method that could be utilized?
The total counts of points in cells should be 10,000.
0 Comments
Accepted Answer
Star Strider
on 9 Aug 2023
Edited: Star Strider
on 9 Aug 2023
EDIT — (8 Aug 2023 at 15:49)
Added this example —
x_hit = 0.5+randn(1E4,1)/3; % Missing Data
y_hit = randn(1E4,1)+10; % Missing Data
x_values = linspace(0,1 , 100);
y_values = linspace(5, 14, 100);
% Create a grid of x and y values
[x_grid, y_grid] = meshgrid(x_values, y_values);
cell_counts = zeros(length(x_values), length(y_values));
N = histcounts2(x_hit, y_hit, x_values, y_values); % Use 'histcounts2'
figure
b = bar3(N);
for k = 1:length(b)
zdata = b(k).ZData;
b(k).CData = zdata;
b(k).FaceColor = 'interp';
end
xticklabels(x_values)
yticklabels(y_values)
colormap(turbo)
colorbar
xlabel('x\_values')
ylabel('y\_values')
zlabel('Counts')
.
2 Comments
Star Strider
on 11 Aug 2023
Win11 crashed again for some sort of ‘azure’ and ‘autopilot’ (expletives deleted). I’m going Linux.
That aside, there is not much in your data.
This is the best I can do with them —
LD1 = load('x_hit.mat');
LD2 = load('y_hit.mat');
x_hit = LD1.x_hit;
y_hit = LD2.y_hit;
% x_hit = 0.5+randn(1E4,1)/3; % Missing Data
% y_hit = randn(1E4,1)+10; % Missing Data
x_values = linspace(0,1 , 100);
y_values = linspace(5, 14, 100);
% Create a grid of x and y values
[x_grid, y_grid] = meshgrid(x_values, y_values);
cell_counts = zeros(length(x_values), length(y_values));
[N,Xedg,Yedg] = histcounts2(x_hit, y_hit, x_values, y_values); % Use 'histcounts2'
Nsz = size(N)
idx = find(N>0);
Xvalue = x_grid(idx);
Yvalue = y_grid(idx);
Cell_Counts = N(idx);
Cell_Freq = Cell_Counts/sum(Cell_Counts);
CountsTable = table(Xvalue, Yvalue, Cell_Counts, Cell_Freq)
figure
b = bar3(N);
for k = 1:length(b)
zdata = b(k).ZData;
b(k).CData = zdata;
b(k).FaceColor = 'interp';
end
Ax = gca;
% Ax.YDir = 'reverse';
xticks(0:20:100)
xt = xticks;
yt = yticks;
xticklabels(linspace(min(x_values), max(x_values),numel(xt)))
yticklabels(linspace(min(y_values), max(y_values),numel(yt)))
colormap(turbo)
colorbar
xlabel('x\_values')
ylabel('y\_values')
zlabel('Counts')
[x_mtx,y_mtx] = ndgrid(x_values(1:end-1), y_values(1:end-1));
z_mtx = griddata(x_mtx(:), y_mtx(:), N(:), x_mtx, y_mtx);
figure
contour(x_mtx, y_mtx, log(z_mtx+0.01))
grid
colormap(turbo)
colorbar
xlabel('X')
ylabel('Y')
title('Contour Plot of 2-D Histogram Data (Logarithmic)')
figure
surfc(x_mtx, y_mtx, z_mtx+0.01, 'EdgeColor','interp')
grid
colormap(turbo)
colorbar
Ax = gca;
Ax.ZScale = 'log';
xlabel('X')
ylabel('Y')
zlabel('Counts')
title('Surface Plot of 2-D Histogram Data')
grid('on')
It is necessary to take the logarithms of the offset counts to plot the smaller cell count values.
.
More Answers (0)
See Also
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!