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;
  1 件のコメント
Gideon Idumah
Gideon Idumah 2019 年 4 月 21 日
Works perfectly.
Thank you.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by