How to count the amount of markers in a figure?

8 views (last 30 days)
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]);

Answers (1)

KSSV
KSSV on 5 May 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 Comments
Image Analyst
Image Analyst on 5 May 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);

Sign in to comment.

Categories

Find more on General Applications in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!