从网格数据中随机采样:如何在 Matlab 中实现这一点?

发布于 2024-11-04 20:13:29 字数 673 浏览 5 评论 0原文

我有一个 200x200 网格数据点。我想从该网格中随机选取 15 个网格点,并将这些网格中的值替换为从如下所示的已知分布中选择的值。所有 15 网格点都根据给定分布分配随机值。

给定的分布是:

Given Distribution
314.52
1232.8
559.93
1541.4
264.2
1170.5
500.97
551.83
842.16
357.3
751.34
583.64
782.54
537.28
210.58
805.27
402.29
872.77
507.83
1595.1

给定的分布由 20 值组成,这些值是网格数据点的一部分。这些20 网格点是固定的,即它们不能是随机选取15 点的一部分。这20个点的坐标是固定的,不应该是随机选取的一部分,它们是:

x   27  180 154 183 124 146 16  184 138 122 192 39  194 129 115 33  47  65  1   93
y   182 81  52  24  168 11  90  153 133 79  183 25  63  107 161 14  65  2   124 79

有人可以帮忙解决如何在Matlab中实现这个问题吗?

I have a 200x200 gridded data points. I want to randomly pick 15 grid points from that grid and replace the values in those grids with values selected from a known distribution shown below. All 15 grid points are assigned random values from the given distribution.

The given distribution is:

Given Distribution
314.52
1232.8
559.93
1541.4
264.2
1170.5
500.97
551.83
842.16
357.3
751.34
583.64
782.54
537.28
210.58
805.27
402.29
872.77
507.83
1595.1

The given distribution is made up from 20 values, which are part of those gridded data points. These 20 grid points are fixed i.e. they must not be part of randomly picking 15 points. The coordinates of these 20 points, which are fixed and should not be part of random picking, are:

x   27  180 154 183 124 146 16  184 138 122 192 39  194 129 115 33  47  65  1   93
y   182 81  52  24  168 11  90  153 133 79  183 25  63  107 161 14  65  2   124 79

Can someone help with how to implement this problem in Matlab?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纵性 2024-11-11 20:13:29

基于 我对你的简单问题的回答,这是一个解决方案,说明如何选择 15 个随机整数点(即 200×200 矩阵中的下标索引)并分配从上面给出的值集中抽取的随机值:

mat = [...];   %# Your 200-by-200 matrix
x = [...];     %# Your 20 x coordinates given above
y = [...];     %# Your 20 y coordinates given above
data = [...];  %# Your 20 data values given above
fixedPoints = [x(:) y(:)];         %# Your 20 points in one 20-by-2 matrix
randomPoints = randi(200,[15 2]);  %# A 15-by-2 matrix of random integers
isRepeated = ismember(randomPoints,fixedPoints,'rows');  %# Find repeated sets of
                                                         %#   coordinates
while any(isRepeated)
  randomPoints(isRepeated,:) = randi(200,[sum(isRepeated) 2]);  %# Create new
                                                                %#   coordinates
  isRepeated(isRepeated) = ismember(randomPoints(isRepeated,:),...
                                    fixedPoints,'rows');  %# Check the new
                                                          %#   coordinates
end
newValueIndex = randi(20,[1 15]);  %# Select 15 random indices into data
linearIndex = sub2ind([200 200],randomPoints(:,1),...
                      randomPoints(:,2));  %# Get a linear index into mat
mat(linearIndex) = data(newValueIndex);    %# Update the 15 points

在上面的代码中,我假设 x 坐标对应于 mat 中的行索引,y 坐标对应于列索引。如果实际上是相反的情况,请将第二个和第三个输入交换到函数 SUB2IND

Building off of my answer to your simpler question, here is a solution for how you can choose 15 random integer points (i.e. subscripted indices into your 200-by-200 matrix) and assign random values drawn from your set of values given above:

mat = [...];   %# Your 200-by-200 matrix
x = [...];     %# Your 20 x coordinates given above
y = [...];     %# Your 20 y coordinates given above
data = [...];  %# Your 20 data values given above
fixedPoints = [x(:) y(:)];         %# Your 20 points in one 20-by-2 matrix
randomPoints = randi(200,[15 2]);  %# A 15-by-2 matrix of random integers
isRepeated = ismember(randomPoints,fixedPoints,'rows');  %# Find repeated sets of
                                                         %#   coordinates
while any(isRepeated)
  randomPoints(isRepeated,:) = randi(200,[sum(isRepeated) 2]);  %# Create new
                                                                %#   coordinates
  isRepeated(isRepeated) = ismember(randomPoints(isRepeated,:),...
                                    fixedPoints,'rows');  %# Check the new
                                                          %#   coordinates
end
newValueIndex = randi(20,[1 15]);  %# Select 15 random indices into data
linearIndex = sub2ind([200 200],randomPoints(:,1),...
                      randomPoints(:,2));  %# Get a linear index into mat
mat(linearIndex) = data(newValueIndex);    %# Update the 15 points

In the above code I'm assuming that the x coordinates correspond to row indices and the y coordinates correspond to column indices into mat. If it's actually the other way around, swap the second and third inputs to the function SUB2IND.

長街聽風 2024-11-11 20:13:29

我认为尤达已经给出了基本的想法。调用 randi 两次以获取要替换的网格坐标,然后将其替换为适当的值。这样做 15 次。

I think yoda already gave the basic idea. Call randi twice to get the grid coordinate to replace, and then replace it with the appropriate value. Do that 15 times.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文