MATLAB: Average of array element at defined interval

average of element of array

Suppose,
a=[1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5]
i want to calculate average of group of five nos. so that my resulting array is like
avg=[3 3 3 3]
Can any one help me for the same.

Best Answer

Use the rehaspe function, then mean, since it takes the means of columns as its default behaviour:
a=[1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5];
ar = reshape(a, [], 4)
ar_mean = mean(ar)
produces:
ar =
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
ar_mean =
3 3 3 3