how can i create matrix of zeros and ones (randomly) sucj that the maximum sum of each row and coloumn =5

1 view (last 30 days)
random matrix(0,1) with specific sum

Accepted Answer

John D'Errico
John D'Errico on 1 Apr 2015
Edited: John D'Errico on 1 Apr 2015
Without knowing the size of your matrix, i.e, the number of unknown elements, this is anything from trivial to do, to very non-trivial, especially if you wish those elements to be uniformly distributed subject to that constraint.
As well, the constraint on both the row and column sums makes things less trivial.
Anyway, for a 5x5 matrix, the answer is trivial, rand(5,5).
For a 100x100 matrix, the answer is very different, and quite un-trivial.
So I'd need some information from you. What size is the matrix?
=======================
Edit: From the comments, we have learned that the matrix is to be 25x25, boolean, with row and column sums that may not exceed 5 for any row or column.
There are several schemes I can think of. The simplest is quite simple really.
A = rand(25,25) > 0.05;
A = triu(A,-2);
A = tril(A,2);
Lets see what this did. spy(A)
As you can see, I created a penta-diagonal array of mostly ones. The relative fraction of ones here is 95% by the way it was constructed, so that will affect the final distribution of ones in the end result.
The trick now is to use randperm.
A = A(randperm(25),randperm(25));
spy(A)
We can verify the distribution of row and column sums.
sum(A,1)
ans =
Columns 1 through 24
5 5 4 5 4 4 3 5 5 4 3 3 5 5 5 4 5 4 4 5 5 5 5 5
Column 25
5
sum(A,2)
ans =
5
4
5
5
5
4
5
5
4
5
5
5
3
5
5
4
5
5
3
4
3
4
5
4
5
As you can see, the row and column sums never exceed 5. The actual distribution of those sums will be determined by the initial random sample. If you wanted fewer ones, then use a larger value on the initial test, instead of 0.05.
If you are worried that the row or column sum can ever exceed 5, think about it. Think about what randperm does and how I transformed that penta-diagonal matrix. Since it starts out never exceeding the row or column sum limit, the permuted array also has the same property!
  5 Comments
John D'Errico
John D'Errico on 1 Apr 2015
There are other schemes I could design, perhaps based on incremental, directed acceptance/rejection sampling methods. But the scheme I've proposed should work quite well, and it will be quite efficient. If this is not acceptable, ask, and I'll see what other scheme I could offer.
Shane Hagen
Shane Hagen on 3 Apr 2015
I have a slightly different issue maybe someone can help?
I have a matrix [signal] of 315954x64 of signal data. In another matrix [FFlash] (155520x1) there is logical 1 or 0 depending on an activation
I have categorized the signal matrix to obtain a matrix [FFsignal] (155520x64) of data when there is an activation
To graph I need matrices of similar dimensions so I wanted to insert the categorized data into a matrix of zeros of size (315954x64)
For example the first group of activation is in rows 631-654 and when categorized I have data for those time points. I want to add this data to a matrix of zeros in the same time points if possible. Therego, zeros until 631-654 and so on through the set. Please help!
I have the question posted also.

Sign in to comment.

More Answers (1)

nhagen
nhagen on 1 Apr 2015
Edited: nhagen on 2 Apr 2015
Hi, this is a lazy way to do it but it's random and it works :
mat=eye(5);
mat=repmat(mat,5,5);
mat=mat(randperm(25),randperm(25));
(all(sum(mat,1)==5)&&all(sum(mat,2)==5)) % Verify the condition

Community Treasure Hunt

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

Start Hunting!