access elements of vector within struct
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hi all,
I want to update the values of a vector, within a struct, recursively, in a for loop. In particular, I am receiving real time inputs(yobs, observations in milliseconds for several hours), for different objects, which I am storing within a struct (S, for example) :
yobs = randn(1,100); %vector of observations, which i am receiving one by one
S = struct;
S.A = nan(1,100); %preallocate for speed
for ii = 1:numel(yobs)
temp = S.A;
temp(ii) = yobs(ii);
S.A = temp;
end
Accessing the struct in this way, requires a copy of S.A into temp, which will be slow since the iterations will be over millions of observations.
Alternatively, I could not preallocate S.A, but that will also consume time. Is it absolutely necessary to copy the previous values of S.A into a temporary variable as above? thank you
0 Commenti
Risposta accettata
Voss
il 22 Apr 2022
"Is it absolutely necessary to copy the previous values of S.A into a temporary variable as above?"
No, it's not necessary.
yobs = randn(1,100); %vector of observations, which i am receiving one by one
S = struct;
S.A = nan(1,100); %preallocate for speed
for ii = 1:numel(yobs)
S.A(ii) = yobs(ii);
end
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!