필터 지우기
필터 지우기

how to make a matrix elements randomly distributed

조회 수: 1 (최근 30일)
Pham Ngoc Thanh
Pham Ngoc Thanh 2013년 6월 21일
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일
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개)

Pham Ngoc Thanh
Pham Ngoc Thanh 2013년 6월 21일
thanks a lot.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by