我想写一个“加权移动窗口”,没有嵌套循环以提高速度。 我已经尝试使用arrayfun而没有获得令人兴奋的结果,但也许我做错了。Matlab - 移动窗口,避免嵌套循环
该窗口在每个位置(存储在B中)具有不同的权重,并应叠加在矩阵A上,返回位于窗口内的矩阵A的值,乘以该位置窗口的权重(读取从B)。另外,窗口可以重叠在另一个上,在这种情况下应该保持最大值。 最后,窗口的尺寸和移位应该是函数的参数。
它看起来更困难,它实际上是,那么我告诉你,我想提高代码:
A = reshape([1:35],7,5)'; % values matrix
B = [1:3;4:6]; % window s weight matrix
% matrices size
[m n] = size(A);
[a b] = size(B);
% window s parameters
shift = 2; % window s movement at each iteration
zone = 3; % window s size (zone x zone)
% preallocation
C = ones(m,n); % to store the right weight to be applied in each position
% loop through positions and find the best weight when they overlap
for i=1:m
for j=1:n
C(i,j) = max(max(B(max(round((i-zone)/shift)+1,1) : min(ceil(i/shift),a) , max(round((j-zone)/shift)+1,1) : min(ceil(j/shift),b))));
end
end
% find the output of the windows
result = C.*A;
我希望我自己清楚,但如果你需要更多的细节请询问。 预先感谢您的帮助!
什么是'zone'和'shift'?发布的代码将不会运行,除非定义了这两个参数。 – Jonas 2011-03-10 17:56:56
对不起,我忘了复制并粘贴这些行。它们是分别定义窗口尺寸(区域x区域)和窗口移位(即:每次迭代中窗口移动)的参数。 我已经更改了代码,现在它应该可以工作。 – Francesco 2011-03-10 18:15:01