Replacing n random zeros in a matrix with a specific nonzero value

Suppose I start with a zero matrix
X = zeros(100)
How can I replace n random zeros in matrix X with a single nonzero value (e.g., replace 10 zero elements in the matrix X with 1)?

 採用された回答

madhan ravi
madhan ravi 2020 年 10 月 2 日

0 投票

X(randperm(numel(X), 10)) = 1

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by