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

6 ビュー (過去 30 日間)
Summer
Summer 2015 年 5 月 13 日
コメント済み: Summer 2015 年 5 月 14 日
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.

採用された回答

Image Analyst
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
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).
Summer
Summer 2015 年 5 月 14 日
Thanks!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by