0

I have a row vector x in Matlab which contains 164372 components. I now want to group these elements in another vector y, which has to contain 52 components. The first element of the vector y must be the average of the first 164372 / 52 = 3161 elements of the vector x, the second element of y must be the average of the next 3161 elements of x, etc. This continues until I have taken all of the 52 averages of the elements in the vector x and placed them in y.

How can I implement this in Matlab? Is there some built-in function that lets me sum elements from a certain index to another index?

Thank you kindly for any help!

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
Kamil
  • 227
  • 4
  • 14

1 Answers1

3

With reshape and mean:

x = rand(1,164372); % example data
N = 52; % block size. Assumed to divide numel(x)
result = mean(reshape(x, numel(x)/N, []), 1)

What this does is: reshape the vector into a 52-row matrix in the usual column-major order, and then compute the mean of each column.

Community
  • 1
  • 1
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147