How to calculate the number of points in each rectangular grid cell

조회 수: 23 (최근 30일)
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.

채택된 답변

Star Strider
Star Strider 2023년 8월 9일
편집: Star Strider 2023년 8월 9일
Perhaps the histcounts2 function could make this easier.
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
Sagar Karmakar
Sagar Karmakar 2023년 8월 11일
편집: Sagar Karmakar 2023년 8월 11일
Hi @Star Strider Thanks for your answer but perhaps this does not work for my data set. Here is my data set....
I aim to determine the count of (x, y) points within individual grid cells. This will allow me to compute the frequency density P for each cell. Subsequently, I intend to create a contour plot of P in the x-y plane.
The challenge with the code provided in the initial question is that although it effectively tallies the points within each cell, the resulting contour plot visualization does not accurately represent high points of P (from the data sets clearly, P should be high at some points)
Thank you!
Star Strider
Star Strider 2023년 8월 11일
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)
Nsz = 1×2
99 99
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)
CountsTable = 17×4 table
Xvalue Yvalue Cell_Counts Cell_Freq _______ ______ ___________ _________ 0.25253 11.727 1 0.0002 0.26263 11.636 104 0.0208 0.42424 10.818 1 0.0002 0.43434 10.727 189 0.0378 0.44444 10.636 429 0.0858 0.45455 10.545 18 0.0036 0.90909 12.364 5 0.001 0.90909 12.455 279 0.0558 0.90909 12.545 335 0.067 0.90909 12.636 6 0.0012 0.91919 12.273 18 0.0036 0.91919 12.364 1639 0.3278 0.91919 12.455 1738 0.3476 0.91919 12.545 24 0.0048 0.92929 12.273 106 0.0212 0.92929 12.364 106 0.0212
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.
.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by