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

2 views (last 30 days)
So I need to generate a matrix of 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 c(x,y), and now from the results of the first run I need Matlab to "randomly" pick (x,y) points with the above condition.
Would appreciate any suggestions on how to go about this.

Accepted Answer

Image Analyst
Image Analyst on 13 May 2015
Edited: Image Analyst on 13 May 2015
Try using thresholding and randperm():
% Generate sample data:
c = randi(40, 9, 9)
% Get a column vector of only those c that are more than 10.
moreThan10 = c(c>10) % Threshold data
% Get 5 indexes at random from moreThan10
randomIndexes = randperm(length(moreThan10), min([5, length(moreThan10)]))
% Extract those 5 random indexes from our list into a new array called output.
output = moreThan10(randomIndexes)
  3 Comments
Image Analyst
Image Analyst on 13 May 2015
Summer, this works fine with 2D matrices. I guess you don't know about linear indexing. If c is a 9 by 9 matrix, you can get to any element by 2D indexing or with a linear index where the numbers start at 1 in the upper left and go down rows, and then across columns until it ends at the last element in the lower right. So c(1) = c(1,1) = upper left, and c(9) = c(9, 1) = lower left. c(73) = c(1, 9) = upper right, and c(81) = c(9,9) = lower right.
No modification to the code is necessary (except for removing the line where I generate sample data, and changing the number of numbers you want to extract from 5 to whatever you want).

Sign in to comment.

More Answers (0)

Categories

Find more on Random Number Generation in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!