how to make a matrix elements randomly distributed

how to make a matrix m elements, which has n elements "one" or #0, randomly distributed, the remaining elements are zero (n<m).

 採用された回答

David Sanchez
David Sanchez 2013 年 6 月 21 日

0 投票

Try this out (adapt the values to your needs):
m = 10;
n = 3;
pos = randperm(m*n,m);
M = zeros(m,n);
M(pos) = 1;
M =
0 0 0
1 1 0
0 1 1
0 0 0
1 0 0
0 0 0
0 0 0
1 0 1
0 1 1
0 0 1
M has the ones randomly distributed.

1 件のコメント

Jan
Jan 2013 年 6 月 21 日
And for older Matlab versions:
M = zeros(m,n);
pos = randperm(m*n);
M(pos(1:n)) = 1;

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

その他の回答 (1 件)

カテゴリ

ヘルプ センター および 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