239 views (last 30 days)

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

Roger Stafford
on 30 Mar 2014

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'.

Brian Crafton
on 3 Dec 2018

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.

Irfan Ahmed
on 11 Apr 2020

I think this should not be element-wise multiplication, instead, it should be A*A'

Walter Roberson
on 29 Mar 2014

When the matrix A is square, (A + A')/2 is symmetric (and positive definite)

John D'Errico
on 22 Mar 2019

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
on 30 Mar 2014

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

mike will
on 22 Mar 2019

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).

John D'Errico
on 22 Mar 2019

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/123643-how-to-create-a-symmetric-random-matrix#comment_205079

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/123643-how-to-create-a-symmetric-random-matrix#comment_205079

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/123643-how-to-create-a-symmetric-random-matrix#comment_205103

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/123643-how-to-create-a-symmetric-random-matrix#comment_205103

Sign in to comment.