How to select a random number from a set of numbers

17 views (last 30 days)
Got a little dilemma. Can you guys help me select a random number from a set of numbers. Using this code,
p = [0.4399,0.4138,0.3888,0.3650,0.3377,0.3120]; pos = randi(length(p)); AirDensity = p(pos)
I can get Matlab to choose one of those numbers from my set of values but it keeps selecting the same numbers over and over again. I think this is because "pos" has been attributed a number in the line before. Is there anyway to get Matlab to choose a different one every time so that only the last line of code needs repeating ?
Many thanks :)
G

Accepted Answer

Sean de Wolski
Sean de Wolski on 26 Dec 2013
doc randperm %?
E.g.
x = sin(1:10);
x(randperm(numel(x),3))

More Answers (1)

Gabriel
Gabriel on 26 Dec 2013
Thanks Sean ! Got it !

Categories

Find more on Creating and Concatenating Matrices 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!