How to create a symmetric random matrix with zero diagonal and with entries just 0 and 1

조회 수: 15 (최근 30일)
There is the follwoing code
d = 1000000*rand(N,1); % The diagonal values
t = triu(bsxfun(@min,d,d.').*rand(N),1); % The upper trianglar random values
M = diag(d)+t+t.'; % Put them together in a symmetric matrix
responding to the question "How to create a symmetric random matrix?" at MathWorks. But my question is a bit different. I was wondering if someone could help about creating a random symmetric random matrix with zero diagonal and with entries just 0 and 1.
Thanks in advance.

채택된 답변

Walter Roberson
Walter Roberson 2021년 6월 5일
편집: Walter Roberson 2021년 6월 5일
N = 10;
temp = randi([0 1], N, N);
d = tril(temp,-1);
M1 = d + d.'
M1 = 10×10
0 0 0 0 1 1 1 0 1 0 0 0 0 0 1 0 1 1 0 0 0 0 0 1 0 0 1 0 1 1 0 0 1 0 1 1 0 1 0 1 1 1 0 1 0 0 1 1 0 1 1 0 0 1 0 0 1 0 0 0 1 1 1 0 1 1 0 0 1 0 0 1 0 1 1 0 0 0 0 1 1 0 1 0 0 0 1 0 0 1 0 0 1 1 1 0 0 1 1 0
M2 = xor(temp, temp.')
M2 = 10×10 logical array
0 0 1 1 1 0 1 0 0 1 0 0 1 0 1 0 1 0 0 0 1 1 0 1 1 0 1 0 1 1 1 0 1 0 0 1 1 0 0 1 1 1 1 0 0 0 1 1 1 0 0 0 0 1 0 0 0 1 1 0 1 1 1 1 1 0 0 0 1 1 0 0 0 0 1 1 0 0 1 1 0 0 1 0 1 1 1 1 0 0 1 0 1 1 0 0 1 1 0 0
  댓글 수: 5
Mojtaba Mohareri
Mojtaba Mohareri 2021년 6월 5일
Understood completely. Thank you very much for your help and consideration.
Mojtaba Mohareri
Mojtaba Mohareri 2021년 6월 5일
I have a related question. When I generate a random matrix (with for example the following code)
bounds = [-15,10];
A = rand(m,m) * range(bounds) + bounds(1)
there is no zero entry in matrix. Could please tell me how can I create a random matrix which has zero entries as well?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by