How do I measure density of random point with a fixed area in MATLAB?

There had a 9x9 area and I have generate a lot of point with this area in random.
How do I measure density of the red point (e.g. Bottom left).
Somebody can give something sample code to let me study with this topic? It is very helpful to my project research.

3 comentarios

hello
you can count the number of points by making this - assuming the data points coordinates are given in a 2 columns matrice (x coordinates = 1st column , y coordinates = 2ndt column
x = rand(1000,1);
y = rand(1000,1);
% area definition
xlow = 0.25;
xhigh = 0.5;
ylow = 0.25;
yhigh = 0.5;
area = (xhigh-xlow)*(yhigh-ylow);
indx = find(x >=xlow & x <=xhigh);
indy = find(y >=ylow & y <=yhigh);
ind = intersect(indx,indy);
plot(x,y,'*g',x(ind),y(ind),'or');
density = numel(ind)/area
As for the code "plot(x,y,'*g',x(ind),y(ind),'or');"
Sorry for what is the '*g' and 'or'?
Dose it is the funtion of matlab or something etc?
hello
these are plot format option * = star, g = green, o = circle , r = red

Iniciar sesión para comentar.

 Respuesta aceptada

Adam Danz
Adam Danz el 20 de En. de 2021
Editada: Adam Danz el 20 de En. de 2021
Use histogram2() or histcounts2() to compute 2D density.
Demo:
xy = randi(400,50,2);
subplot(1,2,1)
plot(xy(:,1), xy(:,2), 'r.')
axis equal
grid on
xlim([1,400])
ylim([1,400])
title('raw data')
binEdges = linspace(1,400,4);
set(gca,'XTick',binEdges, 'YTick',binEdges)
% Compute density in 3X3 bins
subplot(1,2,2)
h = histogram2(xy(:,1), xy(:,2), binEdges,binEdges,...
'DisplayStyle','tile','ShowEmptyBins','on');
axis equal
grid on
xlim([1,400])
ylim([1,400])
title('Density')
set(gca,'XTick',0:100:400, 'YTick',0:100:400)
cb = colorbar();
ylabel(cb,'Density')
% To label counts
[xTxt, yTxt] = ndgrid(h.XBinEdges(2:end)-h.BinWidth(1)/2, ...
h.YBinEdges(2:end)-h.BinWidth(2)/2);
labels = compose('%.0f', h.Values);
hold on
text(xTxt(:), yTxt(:), labels(:), 'VerticalAlignment', 'Middle', 'HorizontalAlignment','Center')

12 comentarios

That worked for me. Thanks Adam
Excuse me how can I exploit the number of density?
such as I want to put the bottom left to 'n' and display the number.
Hi Chun, I don't fully understand your question.
The density values are in h.Values where h is the output to histogram2().
Are you asking how to move the text-numbers to the bottom/left of each grid square?
Thanks for help, actually I want to use the density substitute to the other equation by each area.
I'm sorry, I still don't understand. Could you provide an example?
Sorry for I did not explain clearly, I need to define the number of density in different area and apply to the other equation.
For example the number of desity on bottom left is 5 and I want to put the 5 to "n". Then, substitute n to the equation "n = 2Y ". In the same way, the numer of upper right is 9 it will be put it in "n" and substitute n to the equation "n = 2Y "
Or you can say, I need to use the data of density in the 3X3 bins, but I don't know how to take the data
I see. Take some time to look at the code. The density is stored in h.Values.
Thank you for your patient answers, I found the answer.
n = (h.Values(1));
n2 = (h.Values(2));
n3 = (h.Values(3));
.
.
.
n9 = (h.Values(9));
this is the number of density on 3x3 bins
Actually I am used n = label (1) in the beginning but it is wrong because "label()" does not a number, it just a word. So it can't put it in to any equation.
After calculating the density, how to return the density value to be linked to the coordinate of original point?
For example, return the value "9" to the points in the (1,1) grid...
See h.Values

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Distribution Plots en Centro de ayuda y File Exchange.

Productos

Versión

R2020a

Preguntada:

el 20 de En. de 2021

Comentada:

el 21 de Dic. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by