Generating a random list of (x, y) points that satisfy a condition?
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Summer
am 13 Mai 2015
Kommentiert: Summer
am 14 Mai 2015
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 Kommentare
Akzeptierte Antwort
Image Analyst
am 13 Mai 2015
Bearbeitet: Image Analyst
am 13 Mai 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 Kommentare
Image Analyst
am 13 Mai 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).
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!