How can I generate a random vector between 1-9 with 10 numbers and at least one number should be >2.
Show older comments
X=rand(10,1)*(9-1)+1 How should I change this code to make at least one number >2.
Accepted Answer
More Answers (2)
One way,
X=rand(10,1)*(8-1)+1 +ceil(rand(10,1))
Azzi Abdelmalek
on 21 Dec 2014
X=rand(10,1)*8+1;
idx=X>2;
if nnz(idx)==0
X(randi(numel(X)))=7*rand+2;
end
Categories
Find more on Programming 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!