creating random dense symmetric matrix with desired condition number
6 views (last 30 days)
Show older comments
I want to test my code on a large size matrix which should a dense symmetric with specific condition number. I wonder if there are built-in functions in Matlab for doing that?
0 Comments
Answers (1)
David Goodmanson
on 8 May 2017
Edited: David Goodmanson
on 8 May 2017
[modified to address the first comment below]
Hi othello, I don't know about built-in functions but there are ways to make your own matrices, e.g.
a = 2*rand(10,10)-1 + 2*rand(10,10)-1;
a = a+a'; % symmetric with random entries beween -2 and 2
C = 2345; % desired condition number
[u s v] = svd(a);
s = diag(s); % s is vector
% ===== linear stretch of existing s
s = s(1)*( 1-((C-1)/C)*(s(1)-s)/(s(1)-s(end))) ;
% =====
s = diag(s); % back to matrix
b = u*s*v';
cond(b)
ans = 2.3450e+03
%
%
% ===== log spacing example, where also norm(b) = 1 (induced matrix norm)
% s = exp(linspace(0,-log(C),length(s)));
% =====
The condition number is the ratio between the first and last diagonal entries in matrix s (which are sorted), so s is modified to make that ratio equal to C. This also works if the matrix has complex values and is Hermitian. You can also use normal random variables instead of uniform, etc. Seems to work, although something bad is going to happen eventually if you make C too large.
5 Comments
David Goodmanson
on 10 May 2017
Edited: David Goodmanson
on 10 May 2017
I forgot to mention in comments that I modified the code so now nothing is tbd. Works in every case I tried.
John D'Errico
on 10 May 2017
Edited: John D'Errico
on 10 May 2017
Yes. A complete linear or log transformation of the singular values will suffice. Anything like that will work, where all of the singular values move together. That way it does not allow the singular values to effectively change their sequence. Of course, if the small singular value was exactly zero, then there would be a problem. That event would be unlikely, starting from a random matrix. But even that can be solved. +1 now.
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!