フィルターのクリア

How to create a symmetric random matrix?

109 ビュー (過去 30 日間)
Muhammad Shafique
Muhammad Shafique 2014 年 3 月 29 日
コメント済み: Alaa 2023 年 9 月 3 日
Hello,
I need to create a random matrix meeting the following conditions:
- The values on the main diagonal are between a given range (e.g., 0 to 1000000)
- Each value on the diagonal is randomly distributed/spread in its corresponding row and column vectors.
- The matrix is symmetric (that is to say, corresponding values in upper and lower triangles are the same)
Any help will be highly appreciated.
Best,
M
  2 件のコメント
Roger Stafford
Roger Stafford 2014 年 3 月 30 日
Muhammad, could you please expound at greater length on that second condition: "randomly distributed/spread in its corresponding row and column vectors"? What exactly does that mean? Is that a condition on the sum of the elements in the corresponding row and column, and if so, just what is the condition? What does "randomly distributed/spread" mean in this context? Perhaps a short example of what you have in mind would help.
Muhammad Shafique
Muhammad Shafique 2014 年 3 月 30 日
編集済み: Muhammad Shafique 2014 年 3 月 30 日
Roger, thanks for the question. I meant to say that the values on the row and column must be between 0 and the value on the diagonal. For instance, a random value is chosen within the given range for any element on the diagonal and this value becomes the upper bound of the range for random number generation for the corresponding row/column. This also means that any value in the triangle must not be larger than any of the values on the diagonal that are incident to it.

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

採用された回答

Roger Stafford
Roger Stafford 2014 年 3 月 30 日
Let the random matrix to be generated be called M and its size be NxN.
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
If you want whole numbers, apply the 'floor' function to 'd' and then after computing 't', apply it to 't'.
  3 件のコメント
Youssef  Khmou
Youssef Khmou 2014 年 3 月 30 日
efficient proposition.
Alaa
Alaa 2023 年 9 月 3 日
What if the matrix has definite numbers? (i,e., numbers are not randomly generated)
How can one develop a symmetric matrix (given that the diagonals will be zeros)

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

その他の回答 (4 件)

Brian Crafton
Brian Crafton 2018 年 12 月 3 日
Just came up with this gem and wanted to share it :
A = rand(4)
A .* A'
This will generate a random 4x4 matrix and its clear why.
  2 件のコメント
Irfan Ahmed
Irfan Ahmed 2020 年 4 月 11 日
I think this should not be element-wise multiplication, instead, it should be A*A'
Antong Cheng
Antong Cheng 2022 年 5 月 3 日
The problem is that this only generates positive semi-definite matrices, so it's unfit for certain scenarios.

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


mike will
mike will 2019 年 3 月 22 日
This is the solution:
A = rand(4, 4)
A_symmetric = tril(A) + triu(A', 1)
Where A will be a square matrix, and
tril(A)
returns lower triangular part of matrix A, and
triu(A', 1)
returns upper triangular part of matrix transpose(A).
  1 件のコメント
John D'Errico
John D'Errico 2019 年 3 月 22 日
To be pedantic, Mike has shown ONE solution. Not THE solution. It has different numerical properties from the other solutions shown.

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


Walter Roberson
Walter Roberson 2014 年 3 月 29 日
When the matrix A is square, (A + A')/2 is symmetric (and positive definite)
  1 件のコメント
John D'Errico
John D'Errico 2019 年 3 月 22 日
Actually, the statement shown here is incorrect. Given a square matrix A, (A+A')/2, MAY be positive definiite. But there is no such requirement. For example:
A = randn(4);
As = (A + A')/2;
eig(As)
ans =
-1.9167
-1.6044
-0.37354
2.1428
As is symmetric always. But there is no requirement that it is SPD. As you see, it had 3 negative eigenvalues in this simple example.
Even if rand had been used to generate the matrix, instead of randn, there would still be no assurance the result is positive definite. A counter-example for that took me only one try too.
A = rand(4);
eig((A + A')/2)
ans =
-0.32868
0.088791
0.32729
1.9184
The symmetric computation shown will insure only that the eigenvalues are real. Positive definite requires positivity of the eigenvalues.

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


Youssef  Khmou
Youssef Khmou 2014 年 3 月 30 日
the random matrix is generated using the following :
N=500;
M=rand(N);
M=0.5*(M+M');
L=100; % magnitude
for n=1:N
M(n,n)=L*rand;
end
  2 件のコメント
Muhammad Shafique
Muhammad Shafique 2014 年 3 月 30 日
Youssef, thank you very much for taking the time to help. I'm wondering whether it is possible to get the output in whole numbers.
Ali Waqas
Ali Waqas 2022 年 11 月 10 日
編集済み: Ali Waqas 2022 年 11 月 10 日
floor(M*100) to get whole numbers

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

カテゴリ

Help Center および File ExchangeSparse Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by