Jorgejuan3452
Jorgejuan3452

Reputation: 77

How can I compute a vector with means of another one?

I have a vector A with 400 values, and I need another vector B with 100 values, where every value is the mean of the corresponding 4 values in A. For example, the first value of B would be the mean of the 1st-4th values of A, the second value of B would be the mean of the 5th-8th values of A, and so on. How can I do this in MATLAB?

Thank you very much!

Upvotes: 0

Views: 63

Answers (2)

Dennis Jaheruddin
Dennis Jaheruddin

Reputation: 21563

Here is an alternate solution.

I have expanded it a bit so it will also work if the vector is not an exact multiple of four:

A = 1:399;

B = NaN(4,ceil(length(A)/4));
B(1:length(A))=A;
nanmean(B)

Upvotes: 1

Eitan T
Eitan T

Reputation: 32930

If A is your 400x1 vector, you can reshape it into a matrix with columns of four, and apply mean.

A_means = mean(reshape(A(:), 4, []));

This works because mean operates along columns, if not specified otherwise.

Upvotes: 4

Related Questions