How to assign probability to a matrix element?

조회 수: 2 (최근 30일)
Daniel Maia
Daniel Maia 2013년 12월 11일
편집: Roger Stafford 2013년 12월 11일
Hi,
was wondering if somebody know to do the following:
I want to generate a matrix of zeros and ones with some probability of an element being zero or one. For instance:
Let A be a matrix, then
A(i,j) = 1 with probability p; A(i,j) = 0 with probability 1-p;
for some i,j and 0<p<1.
Is there a function on matlab which I can use to fo this?

채택된 답변

Sean de Wolski
Sean de Wolski 2013년 12월 11일
x = rand(m,n) > p
Just compare a random value to p.

추가 답변 (1개)

Roger Stafford
Roger Stafford 2013년 12월 11일
편집: Roger Stafford 2013년 12월 11일
It isn't clear whether the elements' probabilities are all to be equal to a single value, p, or each element A(i,j) is to have its own individual probability p(i,j). If you mean the latter, let P be a matrix the same size as A in which an element p(i,j) is the probability that A(i,j) is 1, and otherwise it is to be 0. Do this:
A = rand(size(P))<=P;

카테고리

Help CenterFile Exchange에서 Assembly에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by