matlab - Converting mixed empty/non-empty cells into a numeric matrix -
i working on code extract ar(1)-garch(1) parameter, estimated using ar(1)-gjr(1,1) model individual matrices can use them variables in calculations. have 16 time series variables, combine code loop in following way:
for i=1:nindices aa_arch(:,i) = cell2mat(fit{i}.variance.arch)'; end;
my problem variables no aa_arch(:,i) dimension lower nindices. naturally, when try export estimates in loop specified dimension of (:,i) , nindices matlab reports dimension mismatch. tell matlab replace nan 0 instead of leaving spot empty able produce (1,nindices) matrix aa_arch.
i thought of this:
fit{i}.variance.leverage(isnan(fit{i}.variance.leverage))=0
but wasn't able combine part previous code.
i happy hints!
best, carolin
update:
here runnable version of code produces problem. notice code produces dimension mismatch error because there no arch , garch estimate in fit.gjr(1,1) time series 1. these missing values have 0 placeholder in extracted matrix.
returns = randn(2,750)'; t = size(returns,1); nindices = 2; model = arima('ar', nan, 'variance', gjr(1,1)); residuals = nan(t, nindices); variances = nan(t, nindices); fit = cell(nindices,1); options = optimset('fmincon'); options = optimset(options, 'display' , 'off', 'diagnostics', 'off', ... 'algorithm', 'sqp', 'tolcon' , 1e-7); = 1:nindices fit{i} = estimate(model, returns(:,i), 'print', false, 'options', options); [residuals(:,i), variances(:,i)] = infer(fit{i}, returns(:,i)); end i=1:nindices aa_beta(:,i) = cell2mat(fit{i}.ar)'; aa_garch(:,i) = cell2mat(fit{i}.variance.garch)'; aa_arch(:,i) = cell2mat(fit{i}.variance.arch)'; aa_leverage(:,i) = cell2mat(fit{i}.variance.leverage)'; end;
i have general things code, first solution problem:
you can put simple if/else structure in loop handle case of empty array:
for ind1=1:nindices aa_beta(:,ind1) = cell2mat(fit{ind1}.ar)'; %//' %// garch if isempty(cell2mat(fit{ind1}.variance.garch)') %//' aa_garch(1,ind1) = 0; else aa_garch(:,ind1) = cell2mat(fit{ind1}.variance.garch)'; %//' end %// arch (same exact code, should exported function) if isempty(cell2mat(fit{ind1}.variance.arch)') %//' aa_arch(1,ind1) = 0; else aa_arch(:,ind1) = cell2mat(fit{ind1}.variance.arch)'; %//' end aa_leverage(:,ind1) = cell2mat(fit{ind1}.variance.leverage)'; %//' end;
side note: tried this: soz = @(a)isempty(a)*0+~isempty(a)*a;
inline replacement if/else, turns out matlab doesn't handle [] + 0
way wanted (it results in []
instead of 0
; unlike other languages js).
as other things have say:
- i firm supporter of notion 1 shouldn't use
i
,j
loop indices, may cause compatibility problems in cases complex numbers involved (e.g. if loop indexi
1*i
refers loop index instead of square root of-1
). - part of problem arrays writing weren't preallocated - means correct datatype unknown matlab @ time of creation. besides obvious performance hit entails, result in errors 1 encountered here. if, example, used cells
aa_beta
etc. contain empty values, later replace whichever placeholder heart desired using combination ofcellfun
,isempty
. bottom line: lint (aka colorful square on top right of editor window) friend - don't ignore :)
Comments
Post a Comment