Looping command for vectors with common string prefix
1 次查看(过去 30 天)
显示 更早的评论
Dear all,
I need to iterate the same command for a list of vectors with common string prefix and a different number that characterise each. For instance, I have a market average "avilliq" and a list of vectors named milliq1, milliq2, ... , milliq91.
I need to run a MS_Regress_Fit and extract the parameters for each of these vectors. The code for one of them would be something like:
lnavilliq(isnan(lnavilliq))=[];
milliq1(isnan(milliq1))=[];
% Matrix of dependent vars
c = [lnavilliq milliq1];
dep= c; % Defines the dependent variables in system
constVec=ones(length(dep),1);
indep{1}=constVec;
indep{2}=[constVec lnavilliq];
S{1}=[1 1];
S{2}=[1 1 1];
k=2; % Number of states/regimes
[Spec_Out_1]=MS_Regress_Fit(dep,indep,k,S,advOpt);
% Extract vector of parameters (betas)
c_high=Spec_Out_1.param(9);
c_low=Spec_Out_1.param(10);
beta2_high = Spec_Out_1.param(11);
beta2_low = Spec_Out_1.param(12);
%Extracts transition matrix from parameter vector
beta2_p11=Spec_Out_1.Coeff.p(1);
beta2_p22=Spec_Out_1.Coeff.p(4);
I need to iterate Spec_out_# for each milliq# and then extract the parameters c_high_#, c_low_# and so on.
Any suggestion?
Thanks
SG
3 个评论
Stephen23
2018-8-30
编辑:Stephen23
2018-8-30
"Any suggestion?"
Do NOT do that. Magically accessing/defining variabel names is one way that beginners force themselves into writing slow, complex, buggy code that is hard to debug. Read this to know more:
"Where is the problem?"
If the fieldname is PortAvilliq1, then your code will not magically add some number onto the end of the fieldname:
data.PortAvilliq(n)
What your code actually does is accesses n-th element of the field named PortAvilliq. Because this field (probably) does not exist you get an error. See my answer to know how to define the fieldname dynamically.
采纳的回答
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!