# Create random matrix (MATLAB)

21 Ansichten (letzte 30 Tage)
high speed am 15 Jan. 2022
Kommentiert: high speed am 20 Jan. 2022
Dear,
I have these initial parameters :
numRows = 216;
numCols = 432;
A = zeros(numRows,numCols);
numOnesPerCol = randi(([2,3]),[1,numCols]);
numOnesPerRow = randi(([5,6]),[numRows,1]);
and I want to create a binary matrix with dimensions (numRows*numCols) that has numOnesPerCol and numOnesPerRow.
How can I do that please
##### 4 Kommentare3 ältere Kommentare anzeigen3 ältere Kommentare ausblenden
high speed am 15 Jan. 2022
@Torsten I will give you a small example to understand the idea
numRows = 5;
numCols = 10;
A = zeros(numRows,numCols);
numOnesPerCol = randi(([2,3]),[1,numCols]); % Number of ones in each column
numOnesPerRow = randi(([5,6]),[numRows,1]); % Number of ones in each Row
I obtain for example this binary matrix
which contain 5 rows and 10 columns, in each column I have 2 or 3 ones. And in each row I have 5 or 6 ones.

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Torsten am 15 Jan. 2022
Bearbeitet: Torsten am 20 Jan. 2022
Use intlinprog for the program
min: sum_{i=1}^{numRows} (e1_i+ + e1_i-) + sum_{j=1}^{numCols} (e2_j+ + e2_j-)
under the constraints
e1_j+ - e1_j- - sum_{i=1}^{numRows} A(i,j) = -numOnesPerCol(j) (j=1,...,numCols)
e2_i+ - e2_i- - sum_{j=1}^{numCols} A(i,j) = -numOnesPerRow(i) (i=1,...,numRows)
e1_j+, e1_j- >= 0 (j=1,...,numCols)
e2_i+, e2_i- >= 0 (i=1,...,numRows)
A(i,j) in {0,1} for all i,j
##### 6 Kommentare5 ältere Kommentare anzeigen5 ältere Kommentare ausblenden
high speed am 20 Jan. 2022
@Torsten It works with your code. Thank you so much
I really appreciate your help

Melden Sie sich an, um zu kommentieren.

### Weitere Antworten (1)

Image Analyst am 15 Jan. 2022
@high speed I think what you need to do is to first construct a Latin Rectangle:
Then mask it with two numbers like
output = latinRectangle == 1 | latinRectangle == 2;
Sorry I don't have Latin Rectangle code but there is Latin Square, and maybe Latin Rectangle, code in the File Exchange.
##### 1 KommentarKeine anzeigenKeine ausblenden
high speed am 15 Jan. 2022
@Image Analyst Thank you for your response, but I think that I don't need to work with Latin Rectangle.
Because the idea here is to obtain binary matrix that contains a variety of ones between 2 and 3 in each column and a variety of ones between 5 and 6 in each row as in this example

Melden Sie sich an, um zu kommentieren.

### Kategorien

Find more on Linear Least Squares in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by