choose some random number from the given number matlab
1 view (last 30 days)
Patrik Ek on 28 Jul 2014
Edited: Patrik Ek on 28 Jul 2014
Ok, I assume that the example "a=[1 1 2 4 5 1 1 1]; than the answer should be b= 1 3 1" had a typo and that you want to randomly select a number of samples from a vector a. This is how I normally do it.
nSamples = 3;
tmp = rand(size(a));
[~,idx] = sort(tmp);
idx = idx(1:nSamples);
b = a(idx);
This creates a vector of randomly generated numbers, from an uniform distribution and when sorted, the random numbers will be linearly increasing and the original indices will be uniformly distributed.