声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 1678|回复: 9

[编程技巧] 如何产生与几率相关矩阵

[复制链接]
发表于 2010-4-15 11:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
matlab里有没这样的函数?或者可以编程实现以下功能,比如给出一个一维矩阵A,其值介于0--100之间,把这个矩阵二值化为只有0和1两个值的矩阵B,方法为:假设A=[97    79    50    21     3     3    21    50    79    97];我想让生成的矩阵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 编辑 ]
回复
分享到:

使用道具 举报

发表于 2010-4-17 10:02 | 显示全部楼层
A=[97    79    50    21     3     3    21    50    79    97];
c=rand(1,10)*100;B=A>c

评分

1

查看全部评分

发表于 2010-4-17 23:00 | 显示全部楼层

回复 沙发 yufeng 的帖子

请扫盲下
rand出来的数字, 代表几率吗?
发表于 2010-4-19 11:18 | 显示全部楼层
试试这个:

  1. >> A = [97 79  50  21  3  3  21  50  79  97];
  2. >> R = mnrnd(1,[A'/100 1-A'/100]);
  3. >> B = R(:,1)'
  4. B =
  5.      1     1     1     0     0     0     0     1     0     1
复制代码

评分

1

查看全部评分

发表于 2010-4-19 11:55 | 显示全部楼层
现在的版本较旧(无mnrnd函数), 尚无法验证学习!
但谢老师的, 个人有信心, 先加威望了
发表于 2010-4-19 18:35 | 显示全部楼层

回复 5楼 ChaChing 的帖子

谢谢ChaChing大哥的信任和鼓励!
发表于 2010-4-22 16:40 | 显示全部楼层

回复 6楼 xiezhh 的帖子

你好,谢老师,如果我想生成一个100X100的0 1矩阵,其中只有100个1,就是说1的概率是0.1,怎么生成呢?
发表于 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 = [0.2,0.3,0.5];
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
发表于 2010-4-26 00:20 | 显示全部楼层

回复 8楼 dreamstone 的帖子

ls给的就是matlab的help嘛!?
发表于 2010-4-27 08:28 | 显示全部楼层
已经解决了,用randperm

评分

1

查看全部评分

您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-6-16 22:53 , Processed in 0.121361 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表