Specified number of ones in the matrix (column and row)
Afficher commentaires plus anciens
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

Réponse acceptée
Plus de réponses (3)
Bruno Luong
le 2 Déc 2018
Modifié(e) : Bruno Luong
le 2 Déc 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 commentaires
Darek Myszk
le 2 Déc 2018
Bruno Luong
le 2 Déc 2018
Modifié(e) : Bruno Luong
le 2 Déc 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
le 26 Oct 2018
Modifié(e) : John D'Errico
le 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 commentaires
Darek Myszk
le 26 Oct 2018
Modifié(e) : Darek Myszk
le 26 Oct 2018
Bruno Luong
le 26 Oct 2018
Modifié(e) : Bruno Luong
le 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
le 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
le 26 Oct 2018
0 votes
1 commentaire
John D'Errico
le 26 Oct 2018
No need to add an answer just to make a comment.
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!