Convolution (Matrix Multiplication)

30 views (last 30 days)
Mark Wood
Mark Wood on 24 May 2021
Edited: Image Analyst on 24 May 2021
How I want to mutliply one matrix in such way that, For eg,
Given a 5x5 Matrix,
And another 3x3 Matrix,
Mutiply this 3x3 Matrix with 5x5 Matrix such that our resultant matrix look as
Resultant Matrix :
Such That Value of a1 = 31(-1) + 41(-1) + 61(-1) + 71(0) + 41(0) + 71(0) + 71(1) + 61(1) + 41(1) = 40
Value of a2 = 41(-1) + 61(-1) + 71(-1) + 41(0) + 71(0) + 81(0) + 61(1) + 41(1) + 91(1) = 20
Value of a3 = 61(-1) + 71(-1) + 41(-1) + 71(0) + 81(0) + 41(0) + 41(1) + 91(1) + 31(1) = -10
Value of b1 = 71(-1) + 41(-1) + 71(-1) + 71(0) + 61(0) + 41(0) + 31(1) + 31(1) + 51(1) = -70
Similarily we can find all elements of Resultant Matrix
So, Our Resultant Matrix is:
Please help with MATLAB Code of a general case that is valid for all dimensions of both the Matrix.
For e.g., If our given matrix is of 5x5, and our multiplying matrix is of 4x4, then our resultant would become 2x2
Another example, If our given matrix is 6x6, and our multiplying matrix is of 3x3, then our resultant would become 4x4.

Answers (2)

David Hill
David Hill on 24 May 2021
newMatrix=-conv2(a,b,'valid');

Image Analyst
Image Analyst on 24 May 2021
Edited: Image Analyst on 24 May 2021
Try conv2()
output = conv2(m1, m2, 'valid');
Your demo is either showing correlation (not convolution) or else it's showing you happens after the kernel is already flipped. If you're not getting what you want then try imfilter()
output = imfilter(m1, m2, 'valid');
or try to flip the kernel
output conv2(m1, flipud(fliplr(m2)), 'valid');

Tags

Products

Community Treasure Hunt

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

Start Hunting!