Generating a random list of (x, y) points that satisfy a condition?
2 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Image Analyst
2015-5-13
编辑:Image Analyst
2015-5-13
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 个评论
Image Analyst
2015-5-13
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).
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Random Number Generation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!