获得与随机选择的特定浓度值相对应的多个 (x, y) 点

Obtaining a number of (x, y) points that correspond to specific values of concentration that are chosen at random

我需要生成一个点矩阵,前提是它们满足在这些 (x,y) 点浓度大于 10 的条件。请注意,我首先 运行 一个代码让我浓度在每个位置 c(x,y,t),现在根据第一个 运行 的结果,我需要 Matlab "randomly" 根据上述条件选择 (x,y) 点。另请注意第一个 Matlab 运行 结果的维度(随机抽样应基于此):浓度随位置和时间而变化,为 52x61x61,x 为 1x61,y 为 1x52,时间为 1x61 .

例如,对于值为 50 的随机选择的浓度,观察到该值的 x 和 y 是多少?我需要为 12 个不同的点做这个; c(x,y,t) 的 12 个不同值。

我希望我的问题有道理。谢谢。

如果我没有正确理解你的问题。这是一种蛮力方式来做你想做的事。希望能帮助到你。

% dimensions
N_X = 52;
N_Y = 61;
N_T = 61;
% space and time vectors
x = linspace(0,1,N_X);
y = linspace(0,1,N_Y);
t = linspace(0,1,N_T);
% set up a grid
[X Y T]         = meshgrid(y,x,t);
% init concentration as uniform random numbers in [0 100]
c_test      = rand(N_X,N_Y,N_T)*100;

% pick concentrations by some condition
condition   = c_test(:) > 99;
% count how many fulfill that condition
N_true      = sum(condition);

if N_true > 0
    % clear terminal for output
    clc
    % now prune the meshgrids and concentration matrices
    % so only the elements that fulfill the condition are true
    X_out = X(condition);
    Y_out = Y(condition);
    T_out = T(condition);
    C_out = c_test(condition);

    for i=1:12
        % pick one of the remaining elements
        % you may want to pick 12 unique elements ? 
        random_idx  = randi(N_true);
        % print to terminal 
        fprintf('c(%3.2f, %3.2f, %3.2f) = %3.1f\n', X_out(random_idx), Y_out(random_idx), T_out(random_idx), C_out(random_idx))
    end
end

我选择了高于 99% 以表明代码没有找到正确的元素纯属运气:)

这是输出:

c(0.80, 0.88, 0.47) = 99.4
c(0.30, 0.70, 0.65) = 100.0
c(0.10, 0.62, 0.23) = 99.2
c(0.58, 0.14, 0.37) = 99.1
c(0.25, 0.70, 0.98) = 99.5
c(0.20, 0.46, 0.08) = 99.2
c(0.07, 0.40, 0.05) = 99.5
c(0.05, 0.72, 0.82) = 99.8
c(0.10, 0.90, 0.65) = 99.3
c(0.93, 0.26, 0.22) = 99.5
c(0.05, 0.62, 0.12) = 99.6
c(0.85, 0.80, 0.87) = 99.2

嗯,既然你有坐标和力矩 xyt 和集中函数 c,计划是:

  • 计算所有点的浓度;
  • 找出高集中点在哪里;
  • 计算那些高集中点的指数;
  • 使用坐标和矩中的索引来获取位置。

代码应如下所示:

%// Dimensions:
%//   1st --> y
%//   2nd --> x
%//   3rd --> t
conc  = c(x,y,t);
high  = (conc > 50);
indx  = find(high);    

%// Here's the trick: convert back to subscripts the linear indices
%// of the random 12 positions/moments where the concentration was high.
select     = randi(numel(indx), 1, 12);
[yk,xk,tk] = ind2sub(size(high), indx(select));

%// Aggregate the results as (x,y,t) 3x12 numeric array
result = [x(xk); y(yk); t(tk)];