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

2 views (last 30 days)
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 Comments
Stephen23
Stephen23 on 15 Jul 2019
Edited: Stephen23 on 15 Jul 2019
"...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 on 15 Jul 2019
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.

Sign in to comment.

Accepted Answer

Jan
Jan on 15 Jul 2019
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

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!