How to randomly choose 5% of a matrix to be of value 1.

조회 수: 1 (최근 30일)
Gideon Idumah
Gideon Idumah 2019년 4월 21일
댓글: Gideon Idumah 2019년 4월 21일
I have a 256 x 256 matrix which has value 1 inside a small square 20 x 20 around the center, and zero elsewhere. I need to randomly choose say 5% elsewhere to be 1 also. How do I go about this?
Thank you.

채택된 답변

Walter Roberson
Walter Roberson 2019년 4월 21일
candidates = find(~YourMatrix);
ncand = length(candidates);
locs = randperm( ncand, round(ncand * 0.20) );
YourMatrix(locs) = 1;

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by