How to randomly place ones in specified postions of a matrix?

2 ビュー (過去 30 日間)
Muhammad Nabeel Hussain
Muhammad Nabeel Hussain 2019 年 10 月 2 日
I have a matrix.
A=[ 0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0];
I have to randomly place specific number of ones say 8 or 10 in bold zeros positions.
Bold postions may change. Another problem is if i pick 10 positions in this matrix randomly. How to randomly place say 5 ones at those randomly selected 10 positions?
How to do this?

採用された回答

Andrei Bobrov
Andrei Bobrov 2019 年 10 月 2 日
編集済み: Andrei Bobrov 2019 年 10 月 2 日
1.
k = 10;
[m,n] = size(A);
[i1,j1] = ndgrid(1:m,1:2);
[i2,j2] = ndgrid(1:3,3:n);
ii = sub2ind([m,n], [i1(:);i2(:)],[j1(:);j2(:)]);
A(ii(randperm(numel(ii),k))) = 1;
2.
K = 10;
L = 5;
ii = randperm(numel(A),K);
A(ii(randperm(K,L))) = 1;

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by