Trying to run a script many times and combine all the results

조회 수: 1 (최근 30일)
Danny
Danny 2019년 7월 15일
댓글: Danny 2019년 7월 15일
I have a script which takes a random matrix does some calculations on it and then I am interested in looking at the eigenvalues. At the end of my script I get the eigenvalues using e=eig(A) but I want to run this 100s of times and collect and the eigenvalue to plot them on a graph.
I can write for k=1:100 but then e will only give me the eigenvalues for the last calculation as it overwrites e each time, so I think I'm looking for either a way of saying e_k=eig(A) so it will return e_1 e_2 .... or some way of combining them in the script itself so it automatically put them together.
  댓글 수: 2
Stephen23
Stephen23 2019년 7월 15일
편집: Stephen23 2019년 7월 15일
"...so it will return e_1 e_2 ...."
Putting numbers into variable names is a sign that you are doing something wrong.
"..or some way of combining them ..."
Sure that is easy using indexing, either with a numeric array or a cell array: which class would you prefer the output to be?
Danny
Danny 2019년 7월 15일
I think I would be looking for a numeric array as what am I looking for is essentially just a list of all the eigenvalues together, I'm not worried about ordering them in any way.

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

채택된 답변

Jan
Jan 2019년 7월 15일
eList = cell(1, 100);
for k = 1:100
callYourScript;
eList{k} = e;
end
A cell array is useful, if the elements, here e, have different sizes or types. If all e are vectors of the same length, a numerical array is usually more efficient:
eList = zeros(n, 100); % where n is the length of e
for k = 1:100
callYourScript;
eList(:, k) = e;
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by