生成满足条件的 (x, y) 点的随机列表?

Generating a random list of (x, y) points that satisfy a condition?

所以我需要生成一个 x 和 y 点矩阵,前提是它们满足在这些 (x,y) 点浓度大于 10 的条件。请注意,我首先 运行 一个代码让我专注于每个位置,现在我需要 Matlab "randomly" 选取具有上述条件的 (x,y) 点。

如果有任何关于如何解决此问题的建议,我们将不胜感激。

假设您的数据如下所示:

data= [...  x  y  concentration
            1, 1, 1; ...
            2, 1, 11; ...
            1, 2, 12; ...
            2, 2, 1 ...
    ]

您可以找到所有大于 10 的浓度:

data_cbigger10=data(data(:,3)>10,:) % using logical indexing 

并从中选择一个随机点:

randomPoint=data_cbigger10(ceil(size(data_cbigger10,2)*rand),:) % pick a random index

如果尺寸如下:

the dimension of concentration is 52x61x61 as concentration is c(x,y,time), that of x is 1x61 and 1x52 for y. @PetrH – s2015

这应该可以解决问题:

这是你的数据,我随便编的:

x=linspace(0,1,61);
y=linspace(0,1,52);
con=20*rand(61,52);

现在我找到了 con 中大于 10 的所有位置。这导致了一个逻辑矩阵。通过将它与一个相同大小的随机矩阵相乘,我得到一个具有随机值的矩阵,其中 'con' 大于 10,但其他任何地方都为零。

data_cbigger10=rand(size(con)).*(con>10);

通过找到最大值或最小值,选择一个随机点:

for n=1:1:10
    data_cbigger10=rand(size(con)).*(con>10);

    [vals,xind]=max(data_cbigger10);
    xind=squeeze(xind);
    [vals,yind]=max(squeeze(vals));
    [~,time_ind]=max(squeeze(vals));

    yind=yind(time_ind);
    xind=xind(yind,time_ind);
    x_res(n)=x(xind)
    y_res(n)=y(yind)
    time_res(n)=time(time_ind)
    con_res(n)=con(xind,yind,time_ind)
    con(xind,yind,time_ind)=0; % setting the choosen point to zero, so it will not be choosen again.
end

希望这对你有用。

假设您将每个点的浓度 (x,y) 存储在数组 concentration 中,您可以像这样使用 find()randsample() 函数:

conGT10 = find(concentration>10);    % find where concentration is greater than 10 (gives you indices)
randomPoints = randsample(conGT10,nn);    % choose nn random numbers from those that satisfy the condition
x1 = x(randomPoints);     % given the randomly drawn indices pull the corresponding numbers for x and y
y1 = y(randomPoints);

编辑: 以上假定数组 xyconcentration1d 并且长度相同。显然这不是你的问题。

你在 (x,y) 平面上有一个点网格,你测量了不同时间段在这个网格上的浓度。所以x的长度是nxy的长度是nyconcentration的大小是nx乘以ny nt。为简单起见,我假设您只测量一次浓度,即 nt=1 并且浓度只是 2d 数组。

我之前的答案修改后的版本如下:

[rows,cols] = find(concentration>10);    % find where concentration is greater than 10 (gives you indices)
randomIndices = randsample(length(rows),nn);    % choose nn random integers from 1 to n, where n is the number of observations that satisfy the condition 'concentration>10'
randomX = x(rows(randomIndices));
randomY = y(cols(randomIndices));