简体   繁体   中英

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

So I need to generate a matrix of x and y points given that they meet the condition that at these (x,y) points concentration is greater than 10. Note that I first run a code that gives me concentration at each location, and now I need Matlab to "randomly" pick (x,y) points with the above condition.

Would appreciate any suggestions on how to go about this.

assuming your data looks something like this :

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

You could find all concentrations bigger than 10 with:

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

and choose a random point from there with:

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

If the dimensions are as follows:

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

this should do the trick:

This is your data, I just make something up:

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

Now I find all positions in con which are bigger than 10. This results in a logical matrix. By multipling it with an random matrix the same size I get a matrix with random values where 'con' is bigger than 10, but everywhere else equals zero.

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

by finding the max, or min, Value a random point is choosen:

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

Hope this works now for you.

Assuming you have the concentration for each point (x,y) stored in an array concentration you can use the find() and randsample() functions like so:

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);

EDIT: The above assumes that arrays x , y , and concentration are 1d and of the same length. Apparently this is not true for your problem.

You have a grid of points on a (x,y) plane and you measure concentration on this grid in different time periods. So the length of x is nx , the length of y is ny and the size of concentration is nx by ny by nt . For simplicity I will assume that you measure concentration only once, ie nt=1 and concentration is only 2d array.

The modified version of my previous answer would then be as follows:

[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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM