Convolution Shape (full/same/valid) in MATLAB
Last Updated :
11 Nov, 2021
Convolution is a mathematical operation. It is used in Image processing in MatLab. A mask/filter is used to convolve an image for image detection purposes. But MatLab offers three types of convolution. Here we shall explain the simple convolution.
The filter slides over the image matrix from left to right. The corresponding values of matrix and filter are multiplied and added together. The value of the matrix under the central value of the filter is replaced by the result of the convolution operation.
Depending on the nature of the filter sliding over the matrix, MatLab had three different types of convolutions.
Example 1:
Given an image matrix:
1st row->[1 2 4]
2nd row->[2 1 3]
3rd row->[3 2 1]
Given filter:
1st row->[1 0 1]
2nd row->[1 0 1]
3rd row->[1 0 1]
Here we are going to apply the 'same' convolution.
The resultant matrix is:[3 10 3]
[5 14 5]
[3 9 3]
Same Convolution
Now we see the Same Convolution. So in this type of convolution, the size of the resultant matrix is the same as the size of the input matrix. The center of the filter/mask is placed over the first element of the first row. Then the filter is slid over the matrix from the left to the right direction. Once a row is covered, the filter is slid down to the next row and it shifts from left to right side. The matrix is padded with 0's when the filter hangs out of the matrix.
Note: The size of the output matrix = the size of the input matrix.
Example 1: Average convolution
Matrix (3, 4):
1st row->[1 5 2 3]
2nd row->[6 7 10 2]
3rd row->[8 4 10 6]
Averaging Filter (3, 3):
1st row->[0.1111 0.1111 0.1111]
2nd row->[0.1111 0.1111 0.1111]
3rd row->[0.1111 0.1111 0.1111]
Same Convolution result (3, 4):
1st row->[2.1111 3.4444 3.2222 1.8889]
2nd row->[3.4444 5.8889 5.4444 3.6667]
3rd row->[2.7778 5.0000 4.3333 3.1111]
Example 2: Normal convolution
Matrix (3, 4):
Row1->[1 5 2 3]
Row2->[6 7 10 2]
Row3->[8 4 10 6]
Normal Filter (3,3):
Row1->[1 1 1]
Row2->[0 0 0]
Row3->[-1 -1 -1]
Same Convolution result:
Row1->[13 23 19 12]
Row2->[6 14 10 11]
Row3->[-13 -23 -19 -12]
Let's take an example for the Same convolution and apply the mask on that,
Example:
Matlab
% MATLAB code for Same Convolution
% Matrix initialisation;
K = [1 5 2 3;
6 7 10 2;
8 4 10 6];
% Averaging mask creation
mask1=ones(3,3).*1/9;
% Result-1
R1=conv2(K,mask1,'same');
% Random mask
mask2=[1 1 1;
0 0 0;
-1 -1 -1];
% Result-2
R2=conv2(K,mask2,'same');
% Show matrix-K, mask1 and result-1
K
mask1
R1
% Show matrix-K, mask2 and result-2
K
mask2
R2
Output 1:

Output 2:

Valid Convolution
In this type of convolution, the size of the resultant matrix reduces. The filter/mask is placed over the matrix in such a way that no portion of the filter is hanging out. The filter completely hovers over the matrix then it slides from left to right in rows and top to down in columns.
Size of resultant matrix: Size of matrix = N * N Size of filter = n * n Size of output = (N-n+1) * (N-n+1) If the matrix and filter are of 3 * 3 size, then result matrix will be 1*1.
Example:
Matrix (3, 4):
Row 1->[1 5 2 3]
Row 2->[6 7 10 2]
Row 3->[8 4 10 6]
Filter (3,3):
Row 1->[1 1 1]
Row 2->[0 0 0]
Row 3->[-1 -1 -1]
Valid Convolution result:
[14 10]
Number of rows in output matrix = (3 - 3 + 1) = 1
Number of columns in output matrix = (4 - 3 + 1) = 2
Example:
Matlab
% MATLAB code for
% Valid convolution.
% Define matrix -1
matrix1 =[1 2 4 3;
2 1 3 5;
3 2 1 6;
2 3 4 9];
% Define mask1
mask1=[1 1 1;
0 0 0;
1 1 1];
% Apply valid convolution.
result1=conv2(matrix1,mask1,'valid');
% Show matrix, mask and result.
matrix1
mask1
result1
% Define mat-2
matrix2=[1 5 2 3;
6 7 10 2;
8 4 10 6];
% Define mask2
mask2=[1 1 1;
0 0 0;
-1 -1 -1];
% Apply valid convolution.
result2=conv2(matrix2,mask2,'valid');
% Show matrix, mask and result.
matrix2
mask2
result2
Output 1:

Output 2:

Full Convolution
In this type of convolution,
- The first right bottom of the filter will hover top-left element of the matrix. Their product will be computed. The rest of the filter elements will be multiplied by 0 because the matrix is padded with 0 by default. If we are using an average filter then the average of the product will be computed.
- The bottom row of the filter will slide over the matrix from the left to the right direction. The last operation in the first row will be computed when the leftmost element of the bottom row of the filter hovers the rightmost element of the first row of the matrix.
- Thus, after sliding 1st row, the filter will come down by one step and again repeat the same steps.
- The filter will slide until its first (top) row hovers the last (bottom) row of the matrix. Then will slide from left to right till the leftmost element of the filter hovers on the rightmost element of the last row of the matrix.
The size of resultant matrix: Size of matrix = N * N Size of filter = n * n Size of output = (N+n-1) * (N+n-1) If the matrix is 5*5 and filter is 3 * 3 size, then result matrix will be 7*7.
Example 1:
Input matrix:
1st row->[1 2 4 3]
2nd row->[2 1 3 5]
3rd row->[3 2 1 6]
4th row->[2 3 4 9]
Filter:
1st row->[1 0 1]
2nd row->[0 1 0]
3rd row->[1 0 1]
Resultant matrix:
1st row->[1 2 5 5 4 3]
2nd row->[2 2 7 10 6 5]
3rd row->[4 6 10 16 10 9]
4th row->[4 7 13 19 13 14]
5th row->[3 4 7 12 10 6]
6th row->[2 3 6 12 4 9]
Example 2:
Matrix (3, 4):
[1 5 2 3]
[6 7 10 2]
[8 4 10 6]
Filter (3,3):
[1 1 1]
[0 0 0]
[-1 -1 -1]
Full convolution result:
[1 6 8 10 5 3]
[6 13 23 19 12 2]
[7 6 14 10 11 3]
[-6 -13 -23 -19 -12 -2]
[-8 -12 -22 -20 -16 -6]
Rows = (3 + 3 -1) = 5
Columns = (4 + 3 - 1) = 6
Example:
Matlab
% MATLAB code of
% FULL convolution.
% Define mat-1
matrix1=[1 2 4 3;
2 1 3 5;
3 2 1 6;
2 3 4 9];
% Define mask1
mask1=[1 0 1;
0 1 0;
1 0 1];
% Apply FULL convolution.
result1=conv2(matrix1,mask1,'full');
% Show matrix, mask and result.
matrix1
mask1
result1
% Define mat-2
matrix2=[1 5 2 3;
6 7 10 2;
8 4 10 6];
% Define mask2
mask2=[1 1 1;
0 0 0;
-1 -1 -1];
% Apply FULL convolution.
result2=conv2(matrix2,mask2,'full');
% Show matrix, mask and result.
matrix2
mask2
result2
Output 1:

Output 2:

Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem