MATLAB Answers

random integer condition selection

27 ビュー (過去 30 日間)
abdul rehman
abdul rehman 2020 年 7 月 10 日 8:41
コメント済み: John D'Errico 2020 年 7 月 10 日 10:37
r1 = rand((randi(10)),1);
from these 10 random values how can I select only those that are > than 0.5

  0 件のコメント

Sign in to comment.

採用された回答

madhan ravi
madhan ravi 2020 年 7 月 10 日 8:43
Wanted = r1(r1 > .5)

  0 件のコメント

Sign in to comment.

その他の回答 (1 件)

John D'Errico
John D'Errico 2020 年 7 月 10 日 9:30
You want to generate a random number of random numbers? With an additional constraint that all are greater than 0.5? This is far esier than you think. (You had an unnecessary pair of parens in your code.)
r1 = rand(randi(10),1);
So that generate a random length vector of uniform random numbers between 0 and 1. But if you wanted to have all of them be numbers between .5 and 1, this is easy.
r1 = (1 + rand(randi(10),1))/2;
So adding 1 to a number between 0 and 1 makes it lie between 1 and 2. If you then divide by 2, it will fall always betweem .5 and 1.
The result is a vector of random length, where all lie between .5 and 1.

  2 件のコメント

abdul rehman
abdul rehman 2020 年 7 月 10 日 9:44
Actually, I want random numbers list between 0 and 1, and select only those > 0.5. But if I generate list between.05 and 1 so it will not meet the requirement that is 0-1. Can you answer in this direction
John D'Errico
John D'Errico 2020 年 7 月 10 日 10:37
Your stated final requirement is the result must lie between .5 and 1. I showed you how to do that.

Sign in to comment.


Translated by