Averaging every n elements of a vector in matlab

后端 未结 2 1774
你的背包
你的背包 2020-12-19 17:58

I would like to average every 3 values of an vector in Matlab, and then assign the average to the elements that produced it.

Examples:

x=[1:12];
y=%T         


        
2条回答
  •  悲&欢浪女
    2020-12-19 18:28

    You may find the mean of each trio using:

    x = 1:12;
    m = mean(reshape(x, 3, []));

    To duplicate the mean and reshape to match the original vector size, use:

    y = m(ones(3,1), :) % duplicates row vector 3 times
    y = y(:)'; % vector representation of array using linear indices

提交回复
热议问题