Iterating in a dataset encompassing a structure
1 回表示 (過去 30 日間)
古いコメントを表示
Hello all,
I have an issue accessing an array in a dataset table which emcompasses a struct in it.
I want to build an array which contains eigenvectors from different rank during the Johansen test.
I have the following :
basevar = evalin('base', x)
[c,n] = size(basevar)
arrcoef = zeros(n)
[h,pvalue,stat,cValue,mles] = jcitest(basevar,'model','H1', 'lags', 1)
Here i get, a dataset called mles.
Inside this dataset I have :
mles =
r0 r1
t1 [1x1 struct] [1x1 struct]
and when i do mles.r0 I have
paramNames: {5x1 cell}
paramVals: [1x1 struct]
res: [1498x2 double]
EstCov: [2x2 double]
eigVal: 0.0106
eigVec: [2x1 double]
rLL: -1.2002e+03
uLL: -1.1902e+03
Here I want to get the eigVec array.
So i open a loop :
for i = 1:n
coeff = double(pvalue(:,i))
if 0.05 - coeff && 0.05 - coeff > 0
arcoeff(i) = mles.r(i).eigVec
end
end
I can see the issue it comes from this part : mles.r(i), it takes r as a variable which is normal, I want to know how to make this .r0 reactive to an iteration. I tried with {} and putting the name in something like this
a = mles.Properties.VarNames{i}
mles.a.eigVec
But this does not work.
Do you know how can make this reactive to my iterative value i?
thanks
D
0 件のコメント
採用された回答
Matt Tearle
2014 年 10 月 21 日
編集済み: Matt Tearle
2014 年 10 月 21 日
If I understand the problem correctly, you're getting a dataset with variables r0, r1, foo, potato, etc, each one of which contains a struct, and you want to extract the eigVal field from each variable. But the problem is that you can't find a way to loop over the variable names programmatically.
Assuming that's the issue...
Firstly, if you can use 13b or later, you can use tables rather than datasets, which are a bit nicer and allow either named or numeric indexing with {}:
for i = 1:n
arcoeff(i) = mles{:,i}.eigVec;
end
But, either way, you can use dynamic variable referencing in the same way you can with structs:
x = dataset(rand(5,1),rand(5,1))
y = 'Var2'
z = x.(y) % Equivalent to z = x.Var2
So now
vnames = mles.Properties.VarNames;
for i = 1:n
arcoeff(i) = mles.(vnames{i}).eigVec;
end
[Side note: given that the eigenvectors are, well, vectors, I think your indexing will have to be something like arcoeff(:,i) = ...]
[Second side note: the condition if 0.05 - coeff && 0.05 - coeff > 0 isn't well formed: the first part 0.05 - coeff is numeric, so will be converted to logical, which will make it equivalent to 0.05 - coeff > 0 (so you're testing the same thing twice -- "if X && X").]
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!