Vectorize a simple code
显示 更早的评论
Hello, Is there more vectorized way to write this code ? ( and avoid the loops)
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
Thank you in advance
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)
采纳的回答
更多回答(1 个)
Honglei Chen
2017-6-30
I don't know what dimensions you have in C, but this looks like
D = conv2(A,B);
Maybe you can use
D = D(1:size(C,1),1:size(C,2));
to extract the portions you want.
HTH
类别
在 帮助中心 和 File Exchange 中查找有关 App Building 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!