random symmetric logical matrix

1 回表示 (過去 30 日間)
Raviteja
Raviteja 2011 年 9 月 12 日
コメント済み: John D'Errico 2018 年 5 月 21 日
How to generate a random logical symmetric matrix?
(binary symmetric matrix)

採用された回答

Andrei Bobrov
Andrei Bobrov 2011 年 9 月 12 日
a = triu(rand(5));
out = a + tril(a',-1)>.5
variant 2
a = rand(5)
b = a+a'
out = b >= mean(b(:))
  4 件のコメント
Derek O'Connor
Derek O'Connor 2011 年 9 月 12 日
I like variant 3 even better. Concise but not obscure. Excellent!
Walter Roberson
Walter Roberson 2011 年 9 月 12 日
I don't see why the mean() version would work without bias. Suppose that all of the random numbers generated were less than 1/4, so even after adding transpose each total was less than 1/2. Clearly the output for such a matrix should be complete logical 0s. But unless all of the values generated were identical, at least one of the sums is going to be greater than the mean, so if one compares to mean(b(:)) one would get logical 1 in that position. (And if all of the values _are_ identical then they would all be equal to the mean to within roundoff and so the returned result would either be all 0 or all 1 depending which way the mean() rounded.)
It seems to me, then, that for variant 2, instead of comparing to mean(b(:)), one should be comparing to 1
a = rand(5);
b = a + a';
out = b >= 1;
Which can then be made more concise as
a = rand(5);
out = (a+a') >= 1;
Note: the uniform distributions all generate (2^b - 2) different possible numbers, with b=53 for all the generators except one legacy generator that has b=24 . The possible numbers are spaced 2^(-b) apart. The generators cannot generate exactly 0 or exactly 1. If you do a quick counting test, you will see that 0.5 exactly is the first number of the second half of the count, so instead of comparing for > 0.5, you should be comparing for >= 0.5 .

サインインしてコメントする。

その他の回答 (1 件)

kika198513
kika198513 2018 年 5 月 21 日
Hi all! Do you have an idea about how to generate a random symmetric logical matrix having a fixed number of 1s over each row and each column? The final matrix should be huge (60000x60000) with very few 1s (8 within each row and each column). I cannot apply the idea above for two reasons: the huge size of the matrix and the fact that in "out = a + tril(a',-1)>.5" I cannot control the final number of 1s at the levels of rows and columns. Many thanks!
  1 件のコメント
John D'Errico
John D'Errico 2018 年 5 月 21 日
Please don't add an answer just to ask a new question. I will not answer it as such. However, I will answer your question IF you ask the question separately, as a NEW question. This is not that difficult to do.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by