How to count the amount of markers in a figure?

6 visualizaciones (últimos 30 días)
Nick de Groene
Nick de Groene el 4 de Mayo de 2021
Comentada: Nick de Groene el 5 de Mayo de 2021
Hello guys,
For my thesis I am trying to create a figure/picture with a X amount of Stars/Dots/... as i needed this in my experiment. I found a code that is kind of able to do this, but the amount of Stars/Dots is not exactly equal to the amount that if fill in the code. Not sure how to make sure that the amount of stars/dots is exactly equal to the amount I fill in. Thanks in advance!
The code I am using:
x = rand(1, 10000);
y = rand(1, 10000);
minAllowableDistance = 0.05;
numberOfPoints = 50;
% Initialize first point.
keeperX = x(1);
keeperY = y(1);
% Try dropping down more points.
counter = 2;
for k = 2 : numberOfPoints
% Get a trial point.
thisX = x(k);
thisY = y(k);
% See how far is is away from existing keeper points.
distances = sqrt((thisX-keeperX).^2 + (thisY - keeperY).^2);
minDistance = min(distances);
if minDistance >= minAllowableDistance
keeperX(counter) = thisX;
keeperY(counter) = thisY;
counter = counter + 1;
end
end
plot(keeperX, keeperY, 'bx','MarkerSize',9);
axis([-0.1 1.1 -0.1 1.1]);

Respuestas (1)

KSSV
KSSV el 5 de Mayo de 2021
x = rand(1, 10000);
y = rand(1, 10000);
minAllowableDistance = 0.05;
numberOfPoints = 50;
% Initialize first point.
keeperX = zeros([],1) ;
keeperY = zeros([],1) ;
keeperX(1) = x(1);
keeperY(1) = y(1);
% Try dropping down more points.
counter = 1;
for k = 2 : numberOfPoints
% Get a trial point.
thisX = x(k);
thisY = y(k);
% See how far is is away from existing keeper points.
distances = sqrt((thisX-keeperX).^2 + (thisY - keeperY).^2);
minDistance = min(distances);
if minDistance >= minAllowableDistance
counter = counter + 1;
keeperX(counter) = thisX;
keeperY(counter) = thisY;
end
end
plot(keeperX, keeperY, 'bx','MarkerSize',9);
axis([-0.1 1.1 -0.1 1.1]);
  4 comentarios
Image Analyst
Image Analyst el 5 de Mayo de 2021
After your loop, you can count the number of non-zero keeperX values.
indexes = keeperX > 0;
% Extract only the ones we want to plot.
keeperX = keeperX(indexes);
keeperY = keeperY(indexes);
fprintf('Number of points plotted = %d.\n', length(keeperX));
plot(keeperX, keeperY, 'bx','MarkerSize',9);
Nick de Groene
Nick de Groene el 5 de Mayo de 2021
Thanks guys! This is really helpful!

Iniciar sesión para comentar.

Categorías

Más información sobre General Applications en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by