필터 지우기
필터 지우기

how do i generate conditional random number either 0 or 1 ?

조회 수: 1 (최근 30일)
Gourav Sisodiya
Gourav Sisodiya 2019년 5월 21일
편집: madhan ravi 2019년 5월 21일
I have a matrix initialized with all zeros. Next i want to fill it with 1 under certain condition.. If distance between each location to every other location is greater than 0 and less than 2, than there must be chance of filling it with 1, that should be random either 0 or 1, but only after distance condition is satisfied.
How can i do it ? i have tried it as shown in code, but its not working.
dist_mtr = squareform( pdist(locations,'euclidean') );
adj_mtr(dist_mtr > 0 & dist_mtr <= 2*r) = randi([0, 1]);

답변 (1개)

madhan ravi
madhan ravi 2019년 5월 21일
편집: madhan ravi 2019년 5월 21일
adj_mtr(dist_mtr > 0 & dist_mtr <= 2*r) = randi([0, 1],1,nnz(dist_mtr > 0 & dist_mtr <= 2*r));

카테고리

Help CenterFile Exchange에서 Random Number Generation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by