How to create a m*n matrix with 0,1 entries?

조회 수: 3 (최근 30일)
sn at
sn at 2017년 2월 14일
편집: sn at 2017년 2월 14일
In this matrix All entries are zeros except for a single 1 in each row and at most a single 1 in each column?
  댓글 수: 2
John D'Errico
John D'Errico 2017년 2월 14일
Trivial.
eye(m,n)
That fits your requirements.
Do you have some more information on what you really need?
sn at
sn at 2017년 2월 14일
nonzero elements are not necessarily on the main diagonal, in fact they must be randomly distributed: for example like this.
[1 0 0 0
0 0 1 0]

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

채택된 답변

John D'Errico
John D'Errico 2017년 2월 14일
편집: John D'Errico 2017년 2월 14일
Did you say random until we had to prod you? :)
The requirements imply that m<=n.
A = eye(m,n);
A(randperm(m),randperm(n))
That yields a fixed number of ones. If you want the result to sometimes have less than m ones, then just start with a diagonal matrix with some of the diagonal elements as zero. Then you can define the number of non-zeros in advance. You can even choose the number of them randomly.
help diag

추가 답변 (1개)

the cyclist
the cyclist 2017년 2월 14일
One way to do this
eye(m,n)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by