MATLAB Answers

0

Average in a 1D array

Asked by jinang patel on 12 Nov 2019 at 13:53
Latest activity Answered by Guillaume
on 12 Nov 2019 at 13:59
Hello,
Is there a matlab function that allows to take average in an array over 'n' elements:
A=[10 20 30 40]
output=[15 35]
Kind Regards,
Jinang

  0 Comments

Sign in to comment.

Tags

1 Answer

Guillaume
Answer by Guillaume
on 12 Nov 2019 at 13:59
 Accepted Answer

Assumming the number of elements of the vectors is a multiple of n, reshape the vector in columns of n rows and take the mean across the rows:
assert(mod(numel(A), n) == 0, 'number of elements of A is not a multiple of n');
result = mean(reshape(A, n, []), 1)

  0 Comments

Sign in to comment.