在MATLAB中用0到1和1到0的不同概率替换向量的元素

时间:2013-09-30 06:14:59

标签: matlab vector replace

是否有可能将A中的零替换为1,将A中的1替换为0且具有固定但不同的概率?

例如:

A = [0 1 1 0 1 0 1 0]我希望将0替换为1,概率为1/4,将1替换为0,概率为1/3。

我正在尝试这样的事情,但它不适用于概率。我的数组是Sent,它有0和1不均匀分布,但具有一定的概率(3/7 0和4/7 1),这是在Sent变量中捕获的,但现在我需要将其更改为Received,其概率不同。

prob=3/7; 
n=100;
pdatodo=1/3;
pdotoda=1/4;
Sent=rand(n,1)>prob;
Received=Sent;
Sent(Received == 0) = 1>pdotoda; Sent(Received == 1) = 0>pdatodo;

2 个答案:

答案 0 :(得分:2)

A = [0,1,1,0,1,0,1,0]

%// first remember the positions of the orginal 1s and 0s
i0 = find(A==0); 
i1 = find(A==1);

p0to1 = 1/4;
p1to0 = 1/3;

%//Create the replacements vectors that will have the size of the original number of 0s and 1s respectively
r0to1 = rand(size(A(i0))) < p0to1;
r1to0 = rand(size(A(i1))) < p1to0

%//Put the replacement vectors in the correct indices (found at the start)
A(i0(r0to1)) = 1;
A(i1(r1to0)) = 0;

答案 1 :(得分:1)

A = [0,1,1,0,1,0,1,0];
p1to0 = 1/3;
p0to1 = 1/4;

%// Find transition probability for each element
transitionProb = A*p1to0 + (1-A)*p0to1;

%// Flip the bits with corresponding ptransition probability
A = xor(A, rand(size(A)) < transitionProb);

您可以通过将两个概率置为零(期望无变化)和一个(期望所有位被翻转)来测试这一点。