MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by Nishaad
on 30 Sep 2013

Hi,

Is there a possibility to replace the zeros in A by 1, and the 1's in A by zero with fixed probability?

For example:

A = [0,1,1,0,1,0,1,0] and I want to replace 0 by 1 with probability 1/4 and 1 by 0 with probability 1/3. Thank you.

*No products are associated with this question.*

Answer by Chocolate Warrior
on 30 Sep 2013

You can use a for loop and counter. Might not be efficient. But still does the trick. Something like this,

for every element in A: if element in A is 0, and count of 0 is ==4:%this is for probability 1/4 A[i]==1; count=0;

Nishaad
on 30 Sep 2013

Tried something like this, but it replaces every 0 by 1 and every 1 by 0. I have an array, Sent, which is made up of 3/7th 0's and 4/7th 1's.

clear;clc;tic;

prob=3/7;

n=100;

pdatodo=1/3;

pdotoda=1/4;

Sent=rand(n,1)>prob;

Received=Sent;

for k=1:length(Sent)

if Sent(k)==0;

Received(k)=1>pdotoda;

elseif Sent(k)==1;

Received(k)=0>pdatodo; end end

Answer by Roger Stafford
on 30 Sep 2013

You don't need a for-loop. It's a problem in logic.

pdatodo=1/3; pdotoda=1/4; r = rand(size(Sent)); Received = (Sent&(r>pdatodo))|(~Sent&(r<=pdotoda));

## 0 Comments