Store results from multiple loops and parameter values

7 次查看(过去 30 天)
Hi,
I have the following multiple loop
for i=1:length(thetas)
theta = thetas(i); % Utility function
for j=1:length(rhos)
rho = rhos(j);
for ii=1:length(gammas)
gamma = gammas(ii);
[kss]=equilibirum(debt);
end
end
end
where in each step I essentially change some parameter values to get different values for the column vector
kss
for example the vector of parameters I am looping over are:
% define cases
thetas = [1, 1.5];
rhos = [0, 0.99, 2];
gammas = [-1,0,0.76, 0.9, 1] ;
Now, I want to remember (or store) for which combination of parameters i get the values for `kss' .
How could I do this matlab in some easy to understand and easy to export (e.g. in excel) way?

回答(1 个)

Ronit
Ronit 2025-5-30
Hello,
You can store the results in a MATLAB table which is both easy to understand and easy to export to a excel sheet using the "writetable" function. Each row of the table corresponds to a unique combination of "theta", "rho", and "gamma", along with the resulting "kss".
Refer to the following code for better understanding:
thetas = [1, 1.5];
rhos = [0, 0.99, 2];
gammas = [-1, 0, 0.76, 0.9, 1];
results = {};
row = 1;
for i = 1:length(thetas)
theta = thetas(i);
for j = 1:length(rhos)
rho = rhos(j);
for ii = 1:length(gammas)
gamma = gammas(ii);
kss = equilibirum(debt);
% Store parameters and kss as a row
results{row, 1} = theta;
results{row, 2} = rho;
results{row, 3} = gamma;
results{row, 4} = kss;
row = row + 1;
end
end
end
T = cell2table(results, 'VariableNames', {'Theta', 'Rho', 'Gamma', 'KSS'});
% Export to Excel
writetable(T, 'kss_results.xlsx');
For more details on the above functions, please refer to the following documentation pages:
I hope this resolves your query.
Thanks

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by