How can I count the no of data points in circular grid?

1 visualización (últimos 30 días)
Sudhanshu Soman
Sudhanshu Soman el 25 de Feb. de 2016
Editada: Cris LaPierre el 20 de Jul. de 2020
Hello everyone,
I have placed grid in circular shape. I have some data in terms of two column matrix, which represents x and y coordinates respectively for points. As you can see from the figure, I have plotted these data points on circular shape grid. Now these data points(particles) fall in different grid cells. Like different grid cells will have different no of points inside them. I want to calculate, no of particles in every grid cell. It should give me column matrix which gives me the particle count for individual grid cell.
I have tried to get it. Please have a look at code.
The only problem it seems to me is, I have not assigned the cellID properly to individual grid cell. That's why it is not giving me correct count per grid cell. Can anyone please help me out to fix that so that it gives me correct count of particles per grid cell. I am still new to MATLAB and in learning phase of it.
How can I do that?
Thanks in advance.
r = -53;
R = 53;
%import excel data
format long;
filename = 'After 4th element.xlsx';
xlrange ='A9:B19938';
T = xlsread(filename,xlrange);
x = (T(:,1));
y = (T(:,2));
n = numel(x);
v1 = randi( 1, n, 1 ) ;
rad= 27;
nBinsX = 100 ;
nBinsY = 100 ;
xg = linspace( r, R, nBinsX+1 ) ;
yg = linspace( r, R, nBinsY+1 ) ;
[a,b] = meshgrid(xg,yg);
u = a(((a-rad).^2+(b-rad).^2)<=rad^2);
v = b(((a-rad).^2+(b-rad).^2)<=rad^2);
%Creating a circle
figure(1) ; clf ; hold on ;
division_points= 40;
theta = linspace(0,2*pi,division_points+1);
p = (rad*cos(theta))+rad-.5;
q = (rad*sin(theta))+rad-.5;
plot(p,q);
%division points to create grid lines
m = p(1,(1:(division_points/2)+1));
n = q(1,(1:(division_points/2)+1));
n_1 = (floor(((numel(q)-1)/4)))+2;
n_2 = (floor(((numel(q)-1)/4))*3)+2;
n_3 = (floor(((numel(q)-1)/4)))+1;
n_4 = (floor(((numel(q)-1)/4))*3)+3;
N_1 = q(n_1:n_2); % not used in code
N_2 = q([n_4:(end-1) 1:n_3]); % not used in code
%plot grid lines in circle
plot([m;m],[n;-n+2*(rad-.5)] ,'Color', 0.8*[1,1,1])
plot( [n;-n+2*(rad-0.5)],[m;m],'Color', 0.8*[1,1,1])
axis([-10 60 -10 60])
L = numel(xg)/nBinsX;
area= abs(m(2)-m(1))*abs(n(2)-n(1));
i= numel(m);
A = i-1:-2:i-(i);
nCells= floor((2*sum(A))*1.0);
%assign unique cellid individually so that particle falling in one grid cell will
%grouped together and then it will be counted using accumarray
xId = sum( bsxfun( @ge, x, fliplr(m(1:end)) ), 2 ) ;
yId = sum( bsxfun( @ge, y, fliplr(m(1:end)) ), 2 ) ;
cellId = floor((numel(m)/2)*(xId - 1)) + yId ;
labels = arrayfun( @(k)sprintf( '%d', k ), 1:nCells, 'UniformOutput', false ) ;
[X,Y] = meshgrid( (p(1:end-1)+p(2:end))/2, (q(1:end-1)+q(2:end))/2 ) ;
U = X(((X-rad).^2+(Y-rad).^2)<=rad^2);
V = Y(((X-rad).^2+(Y-rad).^2)<=rad^2);
%Plot data points (particle)
plot( x, y, 'r.', 'LineWidth', 2, 'MarkerSize', 8 ) ;
format short g;
%No of particle counts in individual grid cell
blockSum_v1 = accumarray( cellId, v1, [nCells 1] ) ;
fprintf( '\nBlock sum v1 =\n' ) ;
disp( blockSum_v1 ) ;
z=numel(blockSum_v1)
%calculate standard deviation and mean
s=std(blockSum_v1,1)
M= mean(blockSum_v1)
cov = s/M
  1 comentario
Prasannata Bhange
Prasannata Bhange el 20 de Jul. de 2020
Did you sort out this problem?, because even I am working on similar program and found some problem with assigning cell IDs? If yes can you suggest me some way to provide proper cellIDs ?

Iniciar sesión para comentar.

Respuestas (1)

Cris LaPierre
Cris LaPierre el 20 de Jul. de 2020
Editada: Cris LaPierre el 20 de Jul. de 2020
Use the inpolygon function. This will return a logical array of the data points inside your polygon. Sum the logical array to get the number of points.

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by