Reputation: 5
Is there a more elegant way to write this simple code ? The aim is to avoid loops.
I tried with repmat
for indexes but it didnt work
for j=1:size(C,1);
for k=1:size(C,2)
C(j,k)=sum(sum(A([1:j],[1:k]).*B([j:-1:1],[k:-1:1])));
end
end
EDIT
I modified the code
j=1:size(C,1);
k=1:size(C,2);
B2=rot90(B,2);
C(j,k)=sum(sum(A(1:j,1:k).*B2(end+1-j:end,end+1-k:end)));
It's normal that I have the same value on all the matrix C ?
P.S: I understood that conv2 is the more performant function but I just want to understand where is the problem (why (j, k) doesn't vary) and what's the solution (without loop)
Thank you
Upvotes: 0
Views: 58
Reputation: 454
Isn't this just what conv2(A,B,'same') does?
EDIT
I tested it. It's not exactly the same. But upper left part of conv(A,B)
is equal to your C
.
Upvotes: 1