Random Number from a given vector of numbers

1 view (last 30 days)
Israa Ahmed
Israa Ahmed on 26 Aug 2021
Commented: Israa Ahmed on 26 Aug 2021
Given a vector of certain allowed numbers,
how to generate another vector whose elements randomly take values from that vector of numbers?

Accepted Answer

Stephen23
Stephen23 on 26 Aug 2021
Edited: Stephen23 on 26 Aug 2021
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)

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by