N-D convolution
You can control the size of the output of the convn
function. For example, the 'same'
option trims the outer part of the convolution and returns only the central part, which is the same size as the input.
Convolve a random 2-by-3-by-2 array A
with a 2-by-2-by-2 kernel B
. The result is a 3-by-4-by-3 array, which is size(A) + size(B) - 1
.
A = rand(2,3,2); B = 0.25*ones(2,2,2); C = convn(A,B)
C = C(:,:,1) = 0.2037 0.2354 0.1898 0.1581 0.4301 0.6902 0.4426 0.1825 0.2264 0.4548 0.2527 0.0244 C(:,:,2) = 0.2733 0.5444 0.4686 0.1975 0.6365 1.3772 1.2052 0.4645 0.3632 0.8327 0.7366 0.2670 C(:,:,3) = 0.0696 0.3090 0.2788 0.0394 0.2063 0.6869 0.7627 0.2821 0.1367 0.3779 0.4839 0.2426
sizeC = size(A) + size(B) - 1
sizeC = 1×3
3 4 3
Return the central part of the convolution, which is the same size as A
.
C = convn(A,B,'same')
C = C(:,:,1) = 1.3772 1.2052 0.4645 0.8327 0.7366 0.2670 C(:,:,2) = 0.6869 0.7627 0.2821 0.3779 0.4839 0.2426
A
— Input arrayInput array, specified as vector, a matrix, or a multidimensional array.
Data Types: double
| single
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
Complex Number Support: Yes
B
— Second input arraySecond input array, specified as a vector, a matrix, or a multidimensional
array to convolve with A
. The array B
does
not have to be the same size as A
.
Data Types: double
| single
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
Complex Number Support: Yes
shape
— Subsection of convolution'full'
(default) | 'same'
| 'valid'
Subsection of the convolution, specified as one of these values:
'full'
— Return the full
N-D convolution.
'same'
— Return the central
part of the convolution, which is the same size as A
.
'valid'
— Return only parts
of the convolution that are computed without zero-padded edges.
C
— N-D convolutionN-D convolution, returned as a vector, a matrix, or a multidimensional
array. If A
and B
have the same
number of dimensions, the full convolution C = convn(A,B)
has
size size(A)+size(B)-1
.
When one or both of A
and B
are
of type single
, then the output is of type single
.
Otherwise, convn
converts inputs to type double
and
returns type double
.
Data Types: double
| single
For discrete, N-dimensional variables A and B, this equation defines the convolution of A and B:
Each ki runs over all values that lead to legal subscripts of A and B.
Usage notes and limitations:
If shape
is 'full'
(default), then
the inputs A
and B
must not be empty
and only one them can be a tall array.
If shape
is 'same'
or
'valid'
, then B
cannot be a tall
array.
For more information, see Tall Arrays.
This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
You have a modified version of this example. Do you want to open this example with your edits?