MATLAB: How to create a symmetric random matrix

matrixrandom

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

Best Answer

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