MATLAB: How do I generate all square matrices (nxn) which meet these requirements?
Show older comments
How do I generate all square matrices which meet these requirements:
-All entries are either a 0 or 1
-All diagonal entries are 0
-At least a single "1" in each row and column
-Opposite Transposal Entries: IE if A(1,2) = 1, then A(2,1) = 0
Accepted Answer
More Answers (1)
Image Analyst
on 23 Jul 2018
How about this:
n = 5;
output = triu(ones(n,n), 1)
output =
0 1 1 1 1
0 0 1 1 1
0 0 0 1 1
0 0 0 0 1
0 0 0 0 0
It meets your criteria I believe.
8 Comments
VeeVan
on 23 Jul 2018
OCDER
on 23 Jul 2018
n = 5;
output = triu(ones(n,n), 1);
output(2:n+1:end) = output(n+1:n+1:end);
output(n+1:n+1:end) = 0;
VeeVan
on 23 Jul 2018
VeeVan
on 23 Jul 2018
OCDER
on 23 Jul 2018
Hm, I can't think of an obvious way to figure out all the permutations of these 1 and 0 that satisfies condition 3. But due to condition 4, you cannot have any more/less 1's than the current matrix given. So try flipping positions of output
output(x,y) <=> output(y,x) for y ~= x
and check if flippling the 1 and 0 still satisfies condition 3
if all(any(output, 1)) && all(any(output, 2))
disp('this is a valid permutation')
end
you might have to use nchoosek to figure out how many combinations of flips are possible when you flip k = 1, 2, 3, .... (# of 1's) positions.
VeeVan
on 23 Jul 2018
Image Analyst
on 23 Jul 2018
I think you at least have to specify the size of the square. How big is your square? 5 elements wide? A thousand?
Categories
Find more on Logical 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!