Output of the function conv2 is not the size I expected?

6 ビュー (過去 30 日間)
John Thress
John Thress 2019 年 7 月 17 日
コメント済み: Star Strider 2019 年 7 月 17 日
Hello,
I am trying to use the conv2 function to convolute a matrix with a filter for the creation of a convolutional neural network. Take the following 3 by 3 matrix:
A = [1 2 3; 4 5 6; 7 8 9];
and the filter:
B = [1 0; 0 1];
For a stride of 1, my understanding of convolution from the examples I have seen is that the output should be (1*1) + (0*2) + (0*4) + (5*1) = 6 for the first element in the resulting matrix and (1*2) + (0*3) + (0*5) + (1*6) = 8 for the second element and so on until the filter reaches the bottom lower half of A for a final result of
ans = [6 8; 12 14];
when I use the conv2 function my results is:
conv2(A,B) = [1 2 3 0
4 6 8 3
7 12 14 6
0 7 8 9];
where my expected answer is in the middle of the output. Where do these extra numbers come from?

採用された回答

Star Strider
Star Strider 2019 年 7 月 17 日
Reverse the order of the arguments (so that the smaller size matrix is first), then use the 'same' shape argument:
C = conv2(B,A,'same')
produces:
C =
6 8
12 14
  2 件のコメント
John Thress
John Thress 2019 年 7 月 17 日
Thank you so much!
What was the previous operation doing? Or rather what was the reason for those extra numbers?
Star Strider
Star Strider 2019 年 7 月 17 日
As always, my pleasure!
It was giving the default ‘shape’ result of conv2, which is to say it produced the 'full' convolution. Using the 'same' argument with the arguments presented as you originally did:
C = conv2(A,B,'same')
would produce a (3x3) result.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCorrelation and Convolution についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by