多维数组的滑动最大窗口及其平均值 [英] Sliding max window and its average for multi-dimensional arrays

查看:99
本文介绍了多维数组的滑动最大窗口及其平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个60 x 21 x 700矩阵,其中60 x 21代表pressure output x number of frames.我想找到2 x 2窗口,该窗口会生成每帧的最大平均压力并将其存储在新变量中,以便可以对其进行绘制.例如,如果矩阵看起来像这样:

I have a 60 x 21 x 700 matrix, where the 60 x 21 represent a pressure output x number of frames. I want to find the 2 x 2 window that generates the maximum average pressure per frame and store it in a new variable so that it can be plotted. For example, if the matrix looked something like this:

01 02 02 01 01
02 01 01 02 02
02 03 04 04 03
01 02 06 10 05
02 02 08 09 05

2 x 2窗口的最大窗口及其平均值为-

The max window and its average for a 2 x 2 window would be -

06 10
08 09

= 8.25

到目前为止,在寻找解决方案时,我只能找到一种方法来获取最大值(例如,来自上述矩阵的10),但实际上无法解决如何获取最大值的问题.来自没有固定索引可参考的小区域的平均值.我对MATLAB相当陌生,因此如果我错过了某些事情或者只是不正确地理解事情,我深表歉意. 任何帮助或指导将不胜感激.

So far in my search for a solution I've only been able to find a way to get the max value (eg. 10 from the matrix above), but really can't work out how to get the max average from a small area that doesn't have a fixed index to reference. I'm fairly new to MATLAB, so my apologies if I've missed something or am simply not understanding things correctly. Any help or guidance would be much appreciated.

推荐答案

2D数组:对于给定的2D数组输入,您可以使用

2D Arrays: For the given 2D array input, you can use 2D convolution -

%// Perform 2D convolution with a kernel of `2 x 2` size with all ones
conv2_out = conv2(A,ones(2,2),'same')

%// Find starting row-col indices of the window that has the maximum conv value
[~,idx] = max(conv2_out(:))
[R,C] = ind2sub(size(A),idx)

%// Get the window with max convolution value
max_window = A(R:R+1,C:C+1)

%// Get the average of the max window
out =  mean2(max_window)

示例代码的逐步运行-

A =
     1     2     2     1     1
     2     1     1     2     2
     2     3     4     4     3
     1     2     6    10     5
     2     2     8     9     5
conv2_out =
     6     6     6     6     3
     8     9    11    11     5
     8    15    24    22     8
     7    18    33    29    10
     4    10    17    14     5
idx =
    14
R =
     4
C =
     3
max_window =
     6    10
     8     9
out =
         8.25


多维数组:对于多维数组,您需要执行


Multi-dimensional Arrays: For multi-dimensional array case, you need to perform ND convolution -

%// Perform ND convolution with a kernel of 2 x 2 size with all ONES
conv_out = convn(A,ones(2,2),'same')

%// Get the average for all max windows in all frames/slices  
[~,idx] = max(reshape(conv_out,[],size(conv_out,3)),[],1)
max_avg_vals = conv_out([0:size(A,3)-1]*numel(A(:,:,1)) + idx)/4

%// If needed, get the max windows across all dim3 slices/frames
nrows = size(A,1)
start_idx = [0:size(A,3)-1]*numel(A(:,:,1)) + idx
all_idx = bsxfun(@plus,permute(start_idx(:),[3 2 1]),[0 nrows;1 nrows+1])
max_window = A(all_idx)

样本输入,输出-

>> A
A(:,:,1) =
     4     1     9     9
     3     7     5     5
     9     6     1     6
     7     1     1     5
     4     2     2     1
A(:,:,2) =
     9     4     2     2
     3     6     4     5
     3     9     1     1
     6     6     8     8
     5     3     6     4
A(:,:,3) =
     5     5     7     7
     6     1     9     9
     7     7     5     4
     4     1     3     7
     1     9     3     1
>> max_window
max_window(:,:,1) =
     9     9
     5     5
max_window(:,:,2) =
     8     8
     6     4
max_window(:,:,3) =
     7     7
     9     9
>> max_avg_vals
max_avg_vals =
            7          6.5            8

这篇关于多维数组的滑动最大窗口及其平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆