From vector to shorter vector of means in Matlab

Suppose I have a vector x

in Matlab of size 1000. I want to create a vector of y

length 10, where each component contains on average 100 entries from the vector x

.

My attempt is pretty simple

for i=1:10
 y(i) = mean(x((1:100)+(i-1)*100));
end

      

I wonder if there is an assembly in the team or a more elegant solution for this.

+3


source to share


1 answer


You can use the reshape

-function to generate a 2d array and then calculate the average over the correct size.

This works as a replacement for your loop:



y = mean(reshape(x,[100,10]),1);

      

Note. It doesn't matter if x

is a column vector or a row vector.

+4


source







All Articles