Last value of Moving Average in single command

2 vues (au cours des 30 derniers jours)
Inna Pelloso
Inna Pelloso le 29 Juin 2022
Commenté : Voss le 29 Juin 2022
Hi,
I have A = rand(10,1). I am calculating an exponential moving average, B = movavg(A,'exponential',2). I only want the last value of B, i.e. C = B(end).
Is there any way to do this in a single command?
Thank you!
IP

Réponse acceptée

Voss
Voss le 29 Juin 2022
Here's a way:
A = rand(10,1);
% original, for reference:
B = movavg(A,'exponential',2);
C = B(end)
C = 0.8873
% single-line command:
C = subsref(movavg(A,'exponential',2),substruct('()',{numel(A)}))
C = 0.8873
  2 commentaires
Inna Pelloso
Inna Pelloso le 29 Juin 2022
Thank you for showing me the subsref command!
Voss
Voss le 29 Juin 2022
You're welcome!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Timetables in Finance dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by