Looping command for vectors with common string prefix
11 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Stefano Grillini
am 27 Aug. 2018
Bearbeitet: Stephen23
am 30 Aug. 2018
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 Kommentare
Stephen23
am 30 Aug. 2018
Bearbeitet: Stephen23
am 30 Aug. 2018
"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.
Akzeptierte Antwort
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!