代码之家  ›  专栏  ›  技术社区  ›  rth

使用数组中的索引进行平均

  •  0
  • rth  · 技术社区  · 4 年前

    我有时间顺序:行是通道,列是时间点,比如说

    x = [1 2 3 4 5 6 7 8; 9 10 11 12 13 14 15 16; 17 18 19 20 21 22 23 24 ; 25 26 27 28 29 30 31 32]
    x =
    
        1    2    3    4    5    6    7    8
        9   10   11   12   13   14   15   16
       17   18   19   20   21   22   23   24
       25   26   27   28   29   30   31   32
    
    

    我有一个特定时间点的指数,当我想计算 x

    y = [4 5 6]
    y =
       4   5   6
    

    我怎样才能从x中得到一个3D阵列,周围有+-2个点,并且平均通过3D尺寸?总之,我需要平均

     3  4  5           4  5  6       5  6  7
    11 12 13   and    12 13 14 and  13 14 15
    19 20 21          20 21 22      21 22 23
    27 28 29          28 29 39      29 30 31
    

    每个入口。

    0 回复  |  直到 4 年前
        1
  •  1
  •   Marco Torres    4 年前

    因为平均值是所有行的平均值,所以只需获得3个块(1个在前,1个在索引,1个在后)并计算平均值。我没有使用平均值函数,因为它将计算每列的平均值。相反,我只是把3个块加起来,然后除以3。

    % Get the x values
    x = [1 2 3 4 5 6 7 8;...
        9 10 11 12 13 14 15 16;...
        17 18 19 20 21 22 23 24 ;...
        25 26 27 28 29 30 31 32]
    % Define the idx
    idx = [4 5 6]
    % Get the mean matrix. It is the mean of 1 column before
    %  the idx and 1 column after. Since there are 3, divided by 3.
    %              1 before    index     1 after
    MeanMatrix = (x(:,idx-1)+x(:,idx)+x(:,idx+1))./3
    
        2
  •  0
  •   Dharman Aman Gojariya    4 年前

    我认为最好的方法是在阵列上循环如下:

    result = [];
    
    for i = 1:length(y)
        result = [result, mean(x(1:height(x),y(i)-1:y(i)+1), 'all')];
    end
    

    在这里,我们只是使用索引将其拆分为您想要的块,然后计算整个选定块的平均值。