Main Content

convmtx2

2-D convolution matrix

Description

T = convmtx2(H,m,n) returns the convolution matrix T for the matrix H. If X is an m-by-n matrix, then reshape(T*X(:),size(H)+[m n]-1) is the same as conv2(X,H).

example

T = convmtx2(H,[m n]) returns the convolution matrix, where the dimensions m and n are a two-element vector.

Examples

collapse all

Show that, for the convolution matrix T for the matrix H, if X is an m-by-n matrix, then reshape(T*X(:),size(H)+[m n]-1) is the same as conv2(X,H)

Description of first code block

H = ones(3,3)/9; % averaging filter 3-by-3
M = 5;
X = magic(M);
T = convmtx2(H,M,M);
Y1 = reshape(T*X(:), size(H)+[5 5]-1)
Y1 = 7×7

    1.8889    4.5556    4.6667    3.6667    2.6667    2.5556    1.6667
    4.4444    7.6667    8.5556    6.5556    6.7778    5.8889    3.4444
    4.8889    8.7778   11.1111   10.8889   12.8889   10.5556    5.8889
    4.1111    6.6667   11.0000   13.0000   15.0000   10.6667    4.5556
    2.7778    6.7778   13.1111   15.1111   14.8889    8.5556    3.7778
    2.3333    5.6667   10.5556   10.7778    8.7778    3.8889    1.3333
    1.2222    3.2222    6.0000    5.0000    4.0000    1.2222    1.0000

Y2 = conv2(X,H)
Y2 = 7×7

    1.8889    4.5556    4.6667    3.6667    2.6667    2.5556    1.6667
    4.4444    7.6667    8.5556    6.5556    6.7778    5.8889    3.4444
    4.8889    8.7778   11.1111   10.8889   12.8889   10.5556    5.8889
    4.1111    6.6667   11.0000   13.0000   15.0000   10.6667    4.5556
    2.7778    6.7778   13.1111   15.1111   14.8889    8.5556    3.7778
    2.3333    5.6667   10.5556   10.7778    8.7778    3.8889    1.3333
    1.2222    3.2222    6.0000    5.0000    4.0000    1.2222    1.0000

isequal(Y1,Y2) % They are the same.
ans = logical
   0

Input Arguments

collapse all

Input matrix, specified as a numeric array.

Data Types: double

Rows in convolution matrix, specified as a numeric scalar.

Data Types: double

Columns in convolution matrix, specified as a numeric scalar.

Data Types: double

Dimensions of convolution matrix, specified as a two-element vector of the form [m n], where m is the number of rows and n is the number of columns.

Data Types: double

Output Arguments

collapse all

Convolution matrix, returned as a numeric array. The output matrix T is of class sparse. The number of nonzero elements in T is no larger than prod(size(H))*m*n.

Version History

Introduced before R2006a

See Also

| (Signal Processing Toolbox)