How can I mutiply every elements in a matrix by another given kernel matrix and then superpose them accodingly?

1 Ansicht (letzte 30 Tage)
Hi !
I have a matrix A and a matrix B (they are all square matrixs but may be different in size), I want to superpose B with the weights and position of each elements in A (the Bs will overlap with each other) to get C. How can I do this efficently? (this process looks like a convolution in someways..?But I haven't figure out how to rewrite it to be a conventional convolution..)
And here is my code where I use for-loops and some examples that describes the function I am looking forward to:
A=[1,0,0;0,0,0;0,0,0];
B=[1,2,3;4,5,6;7,8,9];
temp=zeros(5);
for m = 1:1:3
for n = 1:1:3
temp(m:1:m+2,n:1:n+2)=temp(m:1:m+2,n:1:n+2)+A(m,n).*B;
end
end
C=temp(2:1:4,2:1:4)
Examples:
%Some examples:
input1:
A =
1 0 0
0 0 0
0 0 0
B =
1 2 3
4 5 6
7 8 9
output1:
C =
5 6 0
8 9 0
0 0 0
----------------------------------
input2:
A =
0 0 0
0 0 0
0 0 1
B =
1 2 3
4 5 6
7 8 9
output2:
C =
0 0 0
0 1 2
0 4 5
------------------------------------------------
input3:
A =
1 0 0
0 0 0
0 0 1
B =
1 2 3
4 5 6
7 8 9
output3
C =
5 6 0
8 10 2
0 4 5
This problem has a Physics backgroud that I have a LED array and each LED bulb projects a set of rings onto a wall in front of the LED array, and I want to know that what the wall looks like when all LEDs are turned on. The matrix A above is the LEDs and the matrix B means the effect that single LED bulb would have on the wall. The matrix C I want to calculate is the summarized effect of all LEDs described in A.
Thank you

Akzeptierte Antwort

David Hill
David Hill am 28 Feb. 2020
I think this would work as long as you specify that the square matrix is odd sized.
C=conv2(A,B);
a=size(A,1);
C=C((a+1)/2:end-(a-1)/2,(a+1)/2:end-(a-1)/2);

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by