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

And for older Matlab versions:
M = zeros(m,n);
pos = randperm(m*n);
M(pos(1:n)) = 1;

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

카테고리

도움말 센터 및 File Exchange에서 Contour Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by