Random Point from meshgrid
조회 수: 9 (최근 30일)
이전 댓글 표시
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
Matt J
2021년 12월 18일
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
2021년 12월 18일
편집: Sargondjani
2021년 12월 18일
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.
채택된 답변
Image Analyst
2021년 12월 18일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!