Specified number of ones in the matrix (column and row)
Show older comments
Hi. I need to create a square matrix filled with random values of 1 and 0. Sum of each row and each column must be equal to the given condition. e.g. matrix: 5x5 ; sum of 1's: 3

Accepted Answer
More Answers (3)
Bruno Luong
on 2 Dec 2018
Edited: Bruno Luong
on 2 Dec 2018
From an idea by Akira Agata
n = 10; % Matrix dimension
k = 4; % row/column sum target of 1s
r = [ones(1,k) zeros(1,n-k)];
c = circshift(r,-k+1);
A = toeplitz(c,r);
A = A(randperm(n),randperm(n))
sum(A,2)
sum(A,1)
2 Comments
Darek Myszk
on 2 Dec 2018
Bruno Luong
on 2 Dec 2018
Edited: Bruno Luong
on 2 Dec 2018
I'm not sure I understand with verbal description, minimum distance of what? What is inear block code?
Edit: I see you mention some function that seems to be in Communication Toolbox. I'm not familiar with it or the goal you want to achieve.
It might be possible that this method of toeplitz matrix is not completely random. I do not put too much though on it.
John D'Errico
on 26 Oct 2018
Edited: John D'Errico
on 26 Oct 2018
Just a quick guess...
This reduces to finding a set of 3 permutations of the numbers 1:5, such that none of them fall in the same place. So we might consider the set defined by:
p =
3 4 5 2 1
4 2 1 3 5
1 3 2 5 4
Now, create your matrix as:
M = eye(5);
M = M(:,p(1,:)) + M(:,p(2,:))+ M(:,p(3,:))
M =
1 0 1 0 1
0 1 1 1 0
1 1 0 1 0
1 1 0 0 1
0 0 1 1 1
So sort of like derangements, but not really. You could actually create M using a more efficient expression, thus with accumarray or sparse, but the way I wrote it may be more intuitively understandable.
Given all that, can you find the array p in a simple way? I can think of at least one simple way.
allperms = perms(1:5);
p = nan(3,5);
for i = 1:3
k = randi(size(allperms,1));
p(i,:) = allperms(k,:);
allperms(any(allperms == p(i,:),2),:) = [];
end
which yields:
p
p =
3 2 1 4 5
5 1 4 3 2
4 3 5 2 1
Note: the above will be problematic if you try to solve the problem for large matrices, since perms will explode. But you did ask for 5x5.
3 Comments
Darek Myszk
on 26 Oct 2018
Edited: Darek Myszk
on 26 Oct 2018
Bruno Luong
on 26 Oct 2018
Edited: Bruno Luong
on 26 Oct 2018
John: If I'm not mistaken, to makes it works the 3 permutations must give different values at the same index.
John D'Errico
on 26 Oct 2018
Yes. Each permutation must be distinct from the others at each location.
As fas as getting a different permutation? That is trivial. As long as you do not reset the random seed before each time through, then randi will give you a different set.
For example, I just re-ran it.
p
p =
2 3 5 4 1
3 2 1 5 4
4 1 2 3 5
Which corresponds to the matrix:
M =
0 1 1 0 1
1 1 1 0 0
1 1 0 1 0
1 0 0 1 1
0 0 1 1 1
Darek Myszk
on 26 Oct 2018
0 votes
1 Comment
John D'Errico
on 26 Oct 2018
No need to add an answer just to make a comment.
Categories
Find more on Creating and Concatenating Matrices 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!