How can I access results from loop, that are stored in structs within a cell?

1 回表示 (過去 30 日間)
Good afernoon,
I have a scrip built to call the same function (named QR) several times (with a loop) and to store it's results into structs within a cell:
first_obs = 100;
last_obs = 110;
results_1 = cell(last_obs,1);
dvar_i = bank_return;
dvar_j = system_return;
ivar = [a b c d e f];
quantile = 0.01;
for i=first_obs:last_obs
[coeff_i, fval_i, t_i, VaR, coeff_j, fval_j, t_j, CoVaR] = QR(dvar_i(1:i), dvar_j(1:i), ivar(1:i,:), quantile, 0);
results{i} = struct('coeff_i', coeff_i', 'fval_i', fval_i, 't_i', t_i', 'VaR', VaR, 'coeff_j', coeff_j', 'fval_j', fval_j, 't_j', t_j', 'CoVaR', CoVaR);
end
results = results';
How can I compare the variables' values obtained in each iteration of the loop?
I have already tried several functions, namely the struct2table and then the cell2table. However, I always get the a first table that have other tables within:
Therefore, I cannot look at all variables' values at the same time.
How can I build a matrix where the rows are the observations (each interation of the loop) and the columns are the values of each variable? Please note that the variables don't have the same size. You can find attached a table that show the way I need my matrix to be.
Thanks in advance!

採用された回答

dpb
dpb 2019 年 9 月 1 日
first_obs = 100;
last_obs = 110;
Nobs=last_obs-first_obs+1; % number rows to compute
coeff_i=zeros(Nobs,3); % preallocate outputs
fval_1=zeros(Nobs,1);
t_i=coeff_i;
VaR=fval_1;
coeff_j=coeff_i;
fval_j=fval_i;
t_j=coeff_i;
CoVaR=fval_1;
dvar_i = bank_return;
dvar_j = system_return;
ivar = [a b c d e f];
quantile = 0.01;
j=0; % initialize counter for loop
for i=first_obs:last_obs
j=j+1; % increment counter
[coeff_i(j,:), fval_i(j,:), t_i(j,:), VaR(j,:), coeff_j(j,:), fval_j(j,:), t_j(j,:), CoVaR(j,:)] = ...
QR(dvar_i(1:i), dvar_j(1:i), ivar(1:i,:), quantile, 0);
end
t=[array2table(coeff_i), array2table(fval_i), ... % create table from the arrays
array2table(t_i), array2table(VaR), ...
array2table(coeff_j), array2table(fval_j), ...
array2table(t_j), array2table(CoVaR)];
will leave you with a table with the various variable names of the arrays as the table variable names, the coeff_x data will be a Nx3 array while the t_x are vectors. You can address any subset of rows/variables and subsets with the arrays at will.
  3 件のコメント
dpb
dpb 2019 年 9 月 1 日
編集済み: dpb 2019 年 9 月 3 日
Well, the 8 is a problem--where does that come from? Your table shows 16 elements for every row.
The 8x1 vs 1x8 is just transpose the RHS...but if the various variables aren't as in your sample table, then have to know what they are and somehow deal with that.
The eight returned elements would match up to there being eight LHS variables but only one element each but there are eight variables by my count on LHS.
Need more context here; what is QR? I presumed it was a function returning eight arrays each corresponding in size to the format of the pdf file layout you posed. That assumption doesn't seem to hold but there's not enough info here for us to debug with.
Bárbara Ferreira
Bárbara Ferreira 2019 年 9 月 5 日
It is working now. The problem was with the function outputs (that need to be transposed) and with the preallocation of the outputs.
Everything is perfect now. Thank you very much for your help!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by