Random Number from a given vector of numbers

Given a vector of certain allowed numbers,
how to generate another vector whose elements randomly take values from that vector of numbers?

 Accepted Answer

V = [-1,23,99,111,1024];
N = 3;
A = V(randi(numel(V),1,N)) % with repetition
A = 1×3
111 111 1024
B = V(randperm(numel(V),N)) % no repetition
B = 1×3
1024 111 23

More Answers (0)

Categories

Find more on Random Number Generation in Help Center and File Exchange

Products

Release

R2021a

Tags

Asked:

on 26 Aug 2021

Commented:

on 20 Apr 2024

Community Treasure Hunt

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

Start Hunting!