How to calculate the means of n adjacent units of a vector?

1 vue (au cours des 30 derniers jours)
Ekin
Ekin le 9 Fév 2015
Commenté : Ekin le 9 Fév 2015
Hi, i have a question about vectors.
For example i have a vector A=[1 2 3 4 5 6 7 8 9 10]T I want to get the mean values of every two adjacent units in this vector. I mean i should get B=[1,5 2,5 3,5 4,5 5,5 6,5 7,5 8,5 9,5]T
1,5=(1+2)/2 , 2,5=(2+3)/2 etc.
or every 3 units i should get: C=[2 3 4 5 6 7 8 9]T
2=(1+2+3)/3 , 3=(2+3+4)/3 etc.
or every 4 units i should get: D=[2,5 3,5 4,5 5,5 6,5 7,5 8,5]T etc.
Is there any way to do that for large vectors by using mean() function easily (not using any loop)?
Thanks in advance

Réponse acceptée

Roger Stafford
Roger Stafford le 9 Fév 2015
If you want the mean of each n successive values in A, to place it in X, do this:
X = mean(hankel(A(1:n),A(n,end)),1);
  5 commentaires
Roger Stafford
Roger Stafford le 9 Fév 2015
Oops, you're right, Guillaume. It should be
X = mean(hankel(A(1:n),A(n:end)),1);
Ekin
Ekin le 9 Fév 2015
Oh, thanks. It works fine for now.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by