简体   繁体   English

Matlab中的函数movmean是否创建了矩阵的所有维度的平均值?

[英]Does the function movmean in Matlab create an average of all dimensions of the matrix?

I am trying to smooth the temporal history of each pixel in my matrix- in other words, trying to smooth each pixel through both 'space' (mxn) and 'time'(third dimension). 我试图平滑矩阵中每个像素的时间历史 - 换句话说,试图通过“空间”(mxn)和“时间”(第三维)平滑每个像素。 I am using the function movmean to create an average of each pixel in time of a 1000x1000x8 matrix. 我使用函数movmean来创建1000x1000x8矩阵的每个像素的平均值。

I am currently using the following code to take an average, using a window size of 5, operating along the third dimension: 我目前正在使用以下代码来获取平均值,使用窗口大小为5,沿第三维操作:

av_matrix = movmean(my_matrix,5,3) av_matrix = movmean(my_matrix,5,3)

This is creating an average as expected, but I'm wondering if the window is just operating in the mxn direction and not taking the average along the third dimension as well. 这是按预期创建平均值,但我想知道窗口是否只是在mxn方向上操作而不是沿着第三维度取平均值。

To compute a moving average along the n dimensions of an n -dimensional array (the "window" is an n -dimensional rectangle), the simplest way is to use convolution (see convn ). 为了计算沿的n维阵列的n的移动平均(“窗口”是一个n维的矩形),最简单的方法是使用卷积 (见convn )。

You need to be careful with edge effects , that is, when the convolution kernel (or n -dimensional window) partially slides out of the data. 你需要注意边缘效应 ,也就是说,当卷积内核(或n维度窗口)部分滑出数据时。 What movmean does is average over the actual data points only. movmean所做的只是对实际数据点的平均值。 To achieve that behaviour you can 要实现这种行为,你可以

  1. compute the sum over the kernel via convolution with the 'same' option; 通过使用'same'选项的卷积计算内核的总和 ; and then 接着
  2. divide each entry by the number of actual data points from which it was computed. 将每个条目除以计算它的实际数据点的数量。 This number can also be obtaind via convolution, namely, applying the kernel to an array of ones. 这个数字也可以通过卷积获得,即将内核应用于1的数组。

So, all you need is: 所以,你所需要的只是:

my_matrix = randi(9,5,5,3); % example 3D array
sz = [3 3 2]; % 3D window size
av_matrix = convn(my_matrix, ones(sz), 'same') ...          % step 1
          ./convn(ones(size(my_matrix)), ones(sz), 'same'); % step 2

Check : 检查

The following examples use 以下示例使用

>> my_matrix
my_matrix(:,:,1) =
     6     8     2     1     8
     4     6     7     9     8
     4     5     1     4     3
     5     5     8     7     9
     3     6     6     4     9
my_matrix(:,:,2) =
     8     8     5     3     6
     8     9     6     9     1
     9     5     6     2     2
     1     7     4     1     2
     5     4     7     4     9
my_matrix(:,:,3) =
     6     5     8     6     6
     1     6     8     6     1
     5     5     1     6     7
     1     1     2     9     8
     1     2     6     1     2

With edge effects: 边缘效应:

>> mean(mean(mean(my_matrix(1:2,1:2,1:2))))
ans =
   7.125000000000000
>> av_matrix(1,1,1)
ans =
   7.125000000000000

Without edge effects: 没有边缘效应:

>> mean(mean(mean(my_matrix(1:3,1:3,1:2))))
ans =
   5.944444444444445

>> av_matrix(2,2,1)
ans =
   5.944444444444445

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM