creating loop and storing eigenvalues from random matrices

조회 수: 6 (최근 30일)
Vivi
Vivi 2012년 6월 29일
Hi,
I am very new to Matlab and I am currently trying to write a code on Matlab for a specific task. I believe I need to write a loop.
I want to generate a 2x2 random covariance matrix so the diagonal entries are in the interval [0,1] and the non-diagonal entries are in the interval [-1,1]. I would like to compute the eigenvalues of this matrix which is straight forward.
Here is the difficult part: But I want to repeat this process for many (say 100 times) random 2x2 covariance matrices. Then for each matrix, I get 2 eigenvalues. I want to be able to store the eigenvalues for all 100 matrices into one vector, giving me a 2*100 = 200-entry vector.
I suspect my code will contain something of the following:
a=-1+2*rand;
A=[rand a; a rand];
v=eig(A)
I am just unsure on how to create a loop with allows me to repeat this task for say 100 times and to store the entries as a big vector.

채택된 답변

Walter Roberson
Walter Roberson 2012년 6월 29일
many_times = 100;
all_v = zeros(2, many_times);
for K = 1 : many_times
a = -1+2*rand;
A = [rand a; a rand];
v = eig(A);
all_v(:, K) = v;
end
  댓글 수: 2
Vivi
Vivi 2012년 6월 29일
Hi Walter, Thanks for the fast reply. I think this is nearly correct. Here I have a 2x100 vector. I would like 1x200 vector. So say for the first eigenvector, the 1st and 2nd entries of all_v is just eigenvalues of the first eigenvector. And the 3rd and 4th entries of all_v are the eigenvalues of the second eigenvector.
I basically want to plot a histogram of these eigenvalues and get an eigenvalue spectra.
Vivi
Vivi 2012년 6월 29일
Ooh, I discovered the reshape function, that did the trick! Thank you Walter, much appreciated!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Linear Algebra에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by