matlab 里怎么随机产生一个对称正定矩阵

2024-12-14 14:27:04
推荐回答(2个)
回答1:

  N=10;%矩阵大小N*N
  select=[5:10,inf];
  a=zeros(N,N);
  for i=1:N
  for j=1:i;
  if j==i
  a(i,j)=inf;
  else
  rnd=rand;
  if rnd>0.3
  a(i,j)=inf;
  a(j,i)=a(i,j);
  else
  b=randperm(length(select));
  a(i,j)=select(b(1));
  a(j,i)=a(i,j);
  end
  end
  end
  end
  disp(a);

回答2:

D = diag(rand(N,1))
U = orth(rand(N,N))
A = U' * D * U