How to create random binary matrix with specified number of ones in each row and column?
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Vadym Kovadlo
am 25 Apr. 2017
Beantwortet: Walter Roberson
am 13 Jan. 2019
Number of raws = M, number of columns = N, number of one's in each row = Nrow, number of one's in each column = Ncolumn.
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 13 Jan. 2019
See https://www.mathworks.com/matlabcentral/answers/439541-how-to-create-a-random-binary-matrix-with-equal-number-of-ones-in-each-column-and-equal-number-of-1#answer_356191 for practical code (but which I do not promise will always work.)
0 Kommentare
Weitere Antworten (1)
Jan
am 25 Apr. 2017
Bearbeitet: Jan
am 25 Apr. 2017
This will not work in general: You cannot e.g. place 2 elements per row and per column in a 5 x 5 matrix:
1 0 0 0 0 % <-- here one 1 is missing and cannot be inserted
1 1 0 0 0
0 1 1 0 0
0 0 1 1 0
0 0 0 1 1
But you can create a 6x6 matrix with 3 ones in each column and row.
1 Kommentar
Walter Roberson
am 13 Jan. 2019
Your example does not appear to be complete.
[0 0 1 1 0; 0 1 1 0 0; 1 1 0 0 0; 1 0 0 0 1; 0 0 0 1 1]
ans =
0 0 1 1 0
0 1 1 0 0
1 1 0 0 0
1 0 0 0 1
0 0 0 1 1
This is a 5 x 5 matrix with two 1's per row and per column.
Furthermore for your original matrix, insert the 1 at the top right corner:
1 0 0 0 1
1 1 0 0 0
0 1 1 0 0
0 0 1 1 0
0 0 0 1 1
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!