Random Point from meshgrid
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Antonio Ciociola
el 18 de Dic. de 2021
Comentada: Image Analyst
el 19 de Dic. de 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 comentarios
Matt J
el 18 de Dic. de 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
el 18 de Dic. de 2021
Editada: Sargondjani
el 18 de Dic. de 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.
Respuesta aceptada
Image Analyst
el 18 de Dic. de 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
mx = mean(X(:))
% Get more than is needed in case we need to get rid of some outside grid.
xr = mx + sdx * randn(10*N, 1)
% Get random Y coordinates.
sdy = range(Y(:))/5
my = mean(Y(:))
% Get more than is needed in case we need to get rid of some outside grid.
yr = my + sdy * randn(10*N, 1)
% 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')
1 comentario
Image Analyst
el 19 de Dic. de 2021
@Antonio Ciociola, are you still there? Did this do what you want?
Más respuestas (0)
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!