Store results from multiple loops and parameter values

조회 수: 5 (최근 30일)
msh
msh 2019년 3월 17일
답변: Ronit 2025년 5월 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