【发布时间】:2011-03-10 16:24:50
【问题描述】:
我正在尝试编写一个没有嵌套循环的“加权移动窗口”以提高速度。 我已经尝试过使用arrayfun,但没有得到令人兴奋的结果,但也许我做错了。
窗口在每个位置都有不同的权重(存储在 B 中),应叠加在矩阵 A 上,返回矩阵 A 位于窗口内的值乘以该位置的窗口权重(从乙)。 此外,窗口可以相互重叠,在这种情况下,应保持最大值。 最后窗口的尺寸和位移应该是函数的参数。
看起来比实际更难,所以我向你展示我想要改进的代码:
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?除非定义了这两个参数,否则您发布的代码将不会运行。 -
对不起,我忘记复制和粘贴这些行了。它们是分别定义窗口尺寸(区域 x 区域)和窗口移动(即:每次迭代中窗口的移动)的参数。我已经更改了代码,现在它应该可以工作了。