![]() |
如何在Matlab中创建一个基于百分比的随机数生成器?
我目前正在使用内置的随机数生成器。
例如 nAsp = randi([512,768],[1,1]); 512是下限,768是上限,随机数生成器从这两个值之间选择一个数字。 我想要的是nAsp有两个范围,但我希望其中一个在25%的时间内被调用,而另一个在75%的时间内被调用。然后插入方程式。有谁知道如何执行此操作,或者在matlab中已经有内置函数? 例如 nAsp = randi([512,768],[1,1]); 25%的时间被称为 nAsp = randi([690,720],[1,1]);被称为75%的时间 [B]回答:[/B] 我假设您的意思是25%的时间是随机的?这是一种简单的方法: if (rand(1) >= 0.25) %# 75% chance of falling into this case nAsp = randi([690 720], [1 1]); else nAsp = randi([512 768], [1 1]); end 如果您知道要生成其中N个,则可以 idx = rand(N,1); nAsp = randi([690 720], [N 1]); nAsp(idx < 0.25) = randi([512 768], [sum(idx < 0.25) 1]); %# replace ~25% of the numbers [url=https://stackoverflow.com/questions/2863320]更多&回答...[/url] |
所有时间均为北京时间。现在的时间是 01:09。 |
Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.