lpp8515 发表于 2010-4-15 11:22

如何产生与几率相关矩阵

matlab里有没这样的函数?或者可以编程实现以下功能,比如给出一个一维矩阵A,其值介于0--100之间,把这个矩阵二值化为只有0和1两个值的矩阵B,方法为:假设A=;我想让生成的矩阵B中的元素个数与矩阵A中的元素个数同(size(A)=size(B)),当A(1)=97时,B(1)有97%的几率等于1,当A(2)=79时,B(2)有79%的几率等于1(即有21%的几率等于0),以此类推。最后生成的B为1*10的一维矩阵,且只包含0和1两个数值。这个怎么实现?还望高手指点。

[ 本帖最后由 ChaChing 于 2010-4-18 11:32 编辑 ]

yufeng 发表于 2010-4-17 10:02

A=;
c=rand(1,10)*100;B=A>c

ChaChing 发表于 2010-4-17 23:00

回复 沙发 yufeng 的帖子

请扫盲下
rand出来的数字, 代表几率吗?

xiezhh 发表于 2010-4-19 11:18

试试这个:

>> A = ;
>> R = mnrnd(1,);
>> B = R(:,1)'
B =
   1   1   1   0   0   0   0   1   0   1

ChaChing 发表于 2010-4-19 11:55

现在的版本较旧(无mnrnd函数), 尚无法验证学习!
但谢老师的, 个人有信心, 先加威望了

xiezhh 发表于 2010-4-19 18:35

回复 5楼 ChaChing 的帖子

谢谢ChaChing大哥的信任和鼓励!

maigicku 发表于 2010-4-22 16:40

回复 6楼 xiezhh 的帖子

你好,谢老师,如果我想生成一个100X100的0 1矩阵,其中只有100个1,就是说1的概率是0.1,怎么生成呢?

dreamstone 发表于 2010-4-22 17:05

回复 7楼 maigicku 的帖子

研究下这个就该差不多了吧
mnrnd

Multinomial random numbers
Syntax

r = mnrnd(n,p)
R = mnrnd(n,p,m)
R = mnrnd(N,P)
Description

r = mnrnd(n,p) returns random values r from the multinomial distribution with parameters n and p. n is a positive integer specifying the number of trials (sample size) for each multinomial outcome. p is a 1-by-k vector of multinomial probabilities, where k is the number of multinomial bins or categories. p must sum to one. (If p does not sum to one, r consists entirely of NaN values.) r is a 1-by-k vector, containing counts for each of the k multinomial bins.

R = mnrnd(n,p,m) returns m random vectors from the multinomial distribution with parameters n and p. R is a m-by-k matrix, where k is the number of multinomial bins or categories. Each row of R corresponds to one multinomial outcome.

R = mnrnd(N,P) generates outcomes from different multinomial distributions. P is a m-by-k matrix, where k is the number of multinomial bins or categories and each of the m rows contains a different set of multinomial probabilities. Each row of P must sum to one. (If any row of P does not sum to one, the corresponding row of R consists entirely of NaN values.) N is a m-by-1 vector of positive integers or a single positive integer (replicated by mnrnd to a m-by-1 vector). R is a m-by-k matrix. Each row of R is generated using the corresponding rows of N and P.
Example

Generate 2 random vectors with the same probabilities:

n = 1e3;
p = ;
R = mnrnd(n,p,2)
R =
   215   282   503
   194   303   503

Generate 2 random vectors with different probabilities:

n = 1e3;
P = [0.2, 0.3, 0.5; ...
   0.3, 0.4, 0.3;];
R = mnrnd(n,P)
R =
   186   290   524
   290   389   321

ChaChing 发表于 2010-4-26 00:20

回复 8楼 dreamstone 的帖子

ls给的就是matlab的help嘛!?

maigicku 发表于 2010-4-27 08:28

已经解决了,用randperm
页: [1]
查看完整版本: 如何产生与几率相关矩阵