【问题标题】:Vectorize 2d convolution on matlab在matlab上矢量化二维卷积
【发布时间】:2016-04-16 22:27:39
【问题描述】:

我得到了这个代码来计算两个给定数组的二维卷积。

[r,c] = size(x);
[m,n] = size(y);
h = rot90(y, 2);
center = floor((size(h)+1)/2);
Rep = zeros(r + m*2-2, c + n*2-2);
return

for x1 = m : m+r-1
for y1 = n : n+r-1
    Rep(x1,y1) = x(x1-m+1, y1-n+1);
end
end

B = zeros(r+m-1,n+c-1);
for x1 = 1 : r+m-1
for y1 = 1 : n+c-1
    for i = 1 : m
        for j = 1 : n
            B(x1, y1) = B(x1, y1) + (Rep(x1+i-1, y1+j-1) * h(i, j));
        end
    end
end
end

如何矢量化它,所以不存在 for 循环? 提前致谢。

【问题讨论】:

  • 这与您的previous post 有何不同?
  • 在上一篇文章中,我试图更笼统,不显示所有代码。
  • 请不要打开重复的问题。如果您想澄清某些事情,请将新信息编辑到现有问题中。
  • 好的。我将删除之前的问题。您知道如何实现这种矢量化吗?
  • 第一个循环只是用零填充输入数组,对吗?我不确定我在那里理解你的数学。对于第二个循环,我们可以使用im2col。我会让那部分工作并检查我的代码。

标签: arrays matlab loops vectorization convolution


【解决方案1】:

这是我想出的:

%// generate test matrices
x = randi(12, 4, 5)
y = [2 2 2;
     2 0 2;
     2 2 2]

[r,c] = size(x);
%[m,n] = size(y);   %// didn't use this
h = rot90(y, 2);
center = floor((size(h)+1)/2);

Rep = zeros(size(x)+size(h)-1);                             %// create image of zeros big enough to pad x
Rep(center(1):center(1)+r-1, center(2):center(2)+c-1) = x;  %// and copy x into the middle

%// all of this can be compressed onto one line, if desired
%// I'm just breaking it out into steps for clarity
CRep = im2col(Rep, size(h), 'sliding');   %// 'sliding' is the default, but just to be explicit
k = h(:);                                 %// turn h into a column vector
BRow = bsxfun(@times, CRep, k);           %// multiply k times each column of CRep
B = reshape(sum(BRow), r, c)              %// take the sum of each column and reshape to match x

T = conv2(Rep, h, 'valid')                %// take the convolution using conv2 to check

assert(isequal(B, T), 'Result did not match conv2.');

以下是示例运行的结果:

x =

   11   12   11    2    8
    5    9    2    3    2
    7    9    3    4    8
    7   10    8    5    4

y =

   2   2   2
   2   0   2
   2   2   2

B =

    52    76    56    52    14
    96   120   106    80    50
    80   102   100    70    36
    52    68    62    54    34

T =

    52    76    56    52    14
    96   120   106    80    50
    80   102   100    70    36
    52    68    62    54    34

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-11
    • 1970-01-01
    • 1970-01-01
    • 2014-03-08
    • 2023-03-29
    • 2014-11-26
    • 2017-02-24
    相关资源
    最近更新 更多