# Manual convolution of 2D square matrices

11 ビュー (過去 30 日間)
Randa 2022 年 12 月 2 日

I am trying to convolute the sum of 2D square matrices by zero padding. The zero padding should be equal to the size difference of the two matrices. i.e a matrix with a size of 5x5 and a second matrix with a size of 3x3, therefore the padded array should be equal to the first matrix’s size with addition 2 columns and 2 rows (post padding) (hence equal to 7). My code works only for the difference greater than 1 and 2. I marked my problem with a comment,hope that I clarified enough.
function final M1=rand(6) M2=eye(4)
n=size(M1)-size(M2);%subtract size M2 from size M1 to get the right output padding size m=size(M2)-1; A=padarray(M1,n,"post"); %padding M1 with the padded size Output=zeros(size(M1)); %output matrix,set it firstly to zero
for i=1:size(A,1)-n for j=1:size(A,2)-n if n >= size(M2) temp=A(i:i+m,j:j+m).*M2; Output(i,j)=sum(temp(:)); elseif m>n temp=A(i:i+m,j:j+m).*M2; %Problem: Index in position 2 exceeds array bounds. Index must not exceed 8. Output(i,j)=sum(temp(:)); else temp=A(i:i+n,j:j+n).*M2; Output(i,j)=sum(temp(:)); end end end display(Output) end
##### 4 件のコメント2 件の古いコメントを表示2 件の古いコメントを非表示
DGM 2022 年 12 月 2 日
Here's some troubleshooting.
% inputs
M1 = rand(6) % this is 6x6
M2 = eye(4) % this is 4x4
% subtract size M2 from size M1 to get the right output padding size
n = size(M1)-size(M2); % [2 2]
m = size(M2)-1; % [3 3]
% A is now 8x8, but if you're corner-centered,
% then you'd need 3px to hang M2 off the edge of M1
% output matrix,set it firstly to zero
Output = zeros(size(M1));
for i = 1:size(A,1)-n
for j = 1:size(A,2)-n
% loop-invariant conditionals should be outside the loop
% are you trying to test i,j to detect edge-approach?
if n >= size(M2)
% test is only true when M1 is at least twice the size of M2 in both dimensions
temp = A(i:i+m,j:j+m).*M2;
Output(i,j) = sum(temp(:));
elseif m>n
% test is only true when m>n in both dimensions
temp = A(i:i+m,j:j+m).*M2;
% Problem: Index in position 2 exceeds array bounds. Index must not exceed 8.
% see note about size of A
% A is size(M1)+n; 6x6 + 2x2 = 8x8
% loop indexing runs up to size(A)-n, which would be size(M1), i.e. 6x6
% the window indexing in these first two cases extends m pixels, i.e. 9x9
Output(i,j) = sum(temp(:));
else
temp = A(i:i+n,j:j+n).*M2;
Output(i,j) = sum(temp(:));
end
end
end
disp(Output)
I'm not sure why all the conditionals are being used. Sometimes that's done as part of a window-truncation routine near the edges, but I don't really see that happening here. If the notes don't help, you might want to clarify what the conditions are intended to test.
If we're assuming M2 is smaller than M1, and that the "center" or indexing point of the window is the upper left pixel (as implied by the indexing A(i:i+m,j:j+m)), then your padding width would be m, not n.
Randa 2022 年 12 月 3 日

Hello, I’m not sure what you mean by edge approach, I’m still new to matlab. I used i and j to move across M1(yes,bigger matrix) and apply the needed calculation. I have tried various matrices sizes and they all worked expect when the zero padding is (n=1 and n=2)and that happens when m>n(that’s my assumption so I might be wrong) m, is mainly used when I have matrices such as (7 and 3) where my n ends up being equal to 4, in this case n is greater than the smaller matrix size. But,I want A to take elements from 1-3(to fit the smaller matrix size) so, i+(size(M2)-1),aka m. I have tried fixating my code from your notes,but I’m still facing the same problem

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

### カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

### Community Treasure Hunt

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

Start Hunting!

Translated by