matrix of zeros and ones within number of zeros in each rows in-between a limit
2 ビュー (過去 30 日間)
古いコメントを表示
want to have a 30x40 matrix of zeros and ones; however in each row total number of 'ones' must be within 10 to 20.
0 件のコメント
採用された回答
Thorsten
2015 年 10 月 9 日
編集済み: Thorsten
2015 年 10 月 9 日
A = zeros(40,30);
Omin = 10; Omax = 20;
for i = 1:size(A, 2)
r = randperm(40);
ind = r(1:(Omin-1) + randi(Omax - Omin +1)) + (i-1)*40;
A(ind) = 1;
end
A = A';
imshow(A)
sum(A,2)
2 件のコメント
Image Analyst
2015 年 10 月 10 日
Go ahead and mark it as "Accepted" then, so he gets reputation points.
その他の回答 (1 件)
John D'Errico
2015 年 10 月 9 日
So generate a matrix with random ones and zeros. Discard any rows that fail this test, and regenerate just those rows. WTP?
Since you have not said what the distribution of the number of ones is, I cannot offer a complete solution, but it is simple in any case. If you will have 17 ones in a row, then generate a row vector with 17 ones, and 23 zeros. Then randomly permute the elements.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Random Number Generation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!