Symmetric binary matrix with diagonal 0 values
显示 更早的评论
I want to create a symmetric (10x10) binary matrix with n number of ones and the diagonal values have to be 0s. Can anyone help me?
1 个评论
James Tursa
2019-10-21
1's can be located anywhere, just not on the diagonal? Random locations? I am assuming n must be even?
采纳的回答
更多回答(1 个)
KALYAN ACHARJYA
2019-10-21
编辑:KALYAN ACHARJYA
2019-10-21
Beware: Before Try (I posted it as answer, so that you may get some idea)
n=input('Eneter the value of n: ')
result=zeros(10,10);
while sum(result)~=n
result=ones(10,10)-diag(diag(ones(10,10))) & randi([0 1],[10 10]);
end
result
When it will works, depending on luck, as I have no idea how to control number on 1 generation in random matrics, but I have forced all diagonal elements to zeros.
During running the Matlab (Execution), you can stop it by pression Ctrl+S
类别
在 帮助中心 和 File Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!