How can I mutiply every elements in a matrix by another given kernel matrix and then superpose them accodingly?
1 回表示 (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
David Hill
2020 年 2 月 28 日
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);
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!