Azzera filtri
Azzera filtri

Random Point from meshgrid

8 visualizzazioni (ultimi 30 giorni)
Antonio Ciociola
Antonio Ciociola il 18 Dic 2021
Commentato: Image Analyst il 19 Dic 2021
Hi everybody, I've a simple problem. I've to pick a certain number "N" of point from a meshgrid.
The constraints of the problem are the following:
  • The number of point must be exactly N;
  • The point must be part of the original set of point given by the meshgrid function;
  • They must be random point from normal distribution (more dense at the center of the matrix and less dense near the boundaries of the matrix);
  • The points need to be different each other;
Here is an example of the original grid that can be used as a starting point. The random point selected must belong to this grid and must satisfy the constraints stated above. Someone can help me? Thank you!
Nc = 20; Nr = 20; dx = 0.2; dy = 0.2;
x = -0.5*Nc*dx:dx:0.5*Nc*dx-dx; y = -0.5*Nr*dy:dy:0.5*Nr*dy-dy;
[X,Y] = meshgrid(x,y);
figure; plot(X,Y,'gs');
  2 Commenti
Matt J
Matt J il 18 Dic 2021
The 3rd requirement can't be reconciled with the others. A finite set cannot be normally distributed. A normal distribution is a continuous distribution.
Sargondjani
Sargondjani il 18 Dic 2021
Modificato: Sargondjani il 18 Dic 2021
I don't know if there is out-of-the-box solution but if i had to program it myself i would do it as follows. And I am sure this is not the most advanced solution, but it would be a start for a beginner.
First try to make it work for 1D:
  • make a mapping of continuous values between 0 and 1 to discrete values of x (ie. gridpoints). Use a distribution that you want. You can now draw a random number, between 0 and 1, and it will give you a gridpoint.
For 2D:
  • basically do the 1D thing twice, once for x, once for y.
  • Add a check whether a grid point was already selected before
  • Repeat draws until you reach N points.
You may also have a look at the function randperm, but im not sure you can tweak the distribution.

Accedi per commentare.

Risposta accettata

Image Analyst
Image Analyst il 18 Dic 2021
I think this will do what you want:
Nc = 20; Nr = 20; dx = 0.2; dy = 0.2;
x = -0.5*Nc*dx:dx:0.5*Nc*dx-dx; y = -0.5*Nr*dy:dy:0.5*Nr*dy-dy;
[X,Y] = meshgrid(x,y);
plot(X,Y,'gs');
hold on;
% Define how many points to take.
N = 50;
% Get random X coordinates.
sdx = range(X(:))/5
sdx = 0.7600
mx = mean(X(:))
mx = -0.1000
% Get more than is needed in case we need to get rid of some outside grid.
xr = mx + sdx * randn(10*N, 1)
xr = 500×1
-0.5417 0.0336 -0.8071 0.5022 -0.4305 -2.0277 -1.0875 0.7109 -0.3767 -0.9617
% Get random Y coordinates.
sdy = range(Y(:))/5
sdy = 0.7600
my = mean(Y(:))
my = -0.1000
% Get more than is needed in case we need to get rid of some outside grid.
yr = my + sdy * randn(10*N, 1)
yr = 500×1
0.5094 -0.0078 0.6382 -0.3101 1.0879 0.2568 -0.7538 -0.3464 -0.7417 0.5768
% Get rid of points outside the grid.
badIndexes = xr < min(x) | xr > max(x);
xr(badIndexes) = [];
xr = xr(1:N); % Extract only the number we actually want.
badIndexes = yr < min(y) | yr > max(y);
yr(badIndexes) = [];
yr = yr(1:N); % Extract only the number we actually want.
% Plot initial coordinates
plot(xr, yr, 'b.', 'MarkerSize', 9)
% Snap the original random points to the closest grid location.
for k = 1 : length(xr)
distances = sqrt((xr(k) - X(:)).^2 + (yr(k) - Y(:)) .^ 2);
[minDistance, indexOfClosest] = min(distances);
% Replace (xr, yr) with grid points.
xr(k) = X(indexOfClosest);
yr(k) = Y(indexOfClosest);
end
% Plot snapped coordinates
plot(xr, yr, 'ms', 'MarkerSize', 9, 'LineWidth', 2)
title('Initial points in blue, final snapped points in magenta')

Più risposte (0)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by