Generating a random list of (x, y) points that satisfy a condition?
조회 수: 6 (최근 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에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!