C = conv2(___,shape) returns
a subsection of the convolution according to shape.
For example, C = conv2(A,B,'same') returns the
central part of the convolution, which is the same size as A.

In applications such as image processing, it can be useful to compare the input of a convolution directly to the output. The conv2 function allows you to control the size of the output.

Create a 3-by-3 random matrix A and a 4-by-4 random matrix B. Compute the full convolution of A and B, which is a 6-by-6 matrix.

The Sobel edge-finding operation uses a 2-D convolution to detect edges in images and other 2-D data.

Create and plot a 2-D pedestal with interior height equal to one.

A = zeros(10);
A(3:7,3:7) = ones(5);
mesh(A)

Convolve the rows of A with the vector u, and then convolve the rows of the result with the vector v. The convolution extracts the horizontal edges of the pedestal.

u = [1 0 -1]';
v = [1 2 1];
Ch = conv2(u,v,A);
mesh(Ch)

To extract the vertical edges of the pedestal, reverse the order of convolution with u and v.

Cv = conv2(v,u,A);
mesh(Cv)

Compute and plot the combined edges of the pedestal.

2-D convolution, returned as a vector or matrix. When A and B are
matrices, then the convolution C = conv2(A,B) has
size size(A)+size(B)-1. When [m,n] = size(A), p
= length(u), and q = length(v), then
the convolution C = conv2(u,v,A) has m+p-1 rows
and n+q-1 columns.

When one or more input arguments to conv2 are
of type single, then the output is of type single.
Otherwise, conv2 converts inputs to type double and
returns type double.

You can also select a web site from the following list:

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.