Does MATLAB has matrix convolution function
显示 更早的评论
I know that MATLAB has a conv(u,v) function that can conduct convolution. Usually u are v are supposed to be vectors of real numbers or complex numbers. Does this function accept matrix input, i.e., u and v are both a sequence of matrices? If it can not, is there any function in MATLAB that can do this job? Thanks!
采纳的回答
更多回答(3 个)
Below is a way you could reduce it to 1 loop, using FEX: mtimesx. In my example, I assume that u is 2x2xM and v is 2xN, i.e., u(:,:,i) are the sequence of matrices and v(:,j) are the sequence of vectors.
%%Fake data
M=5;
N=4;
u=repmat(eye(2),[1,1,M]);
v=ones(2,N);
%%Engine
T=permute(mtimesx(u,v),[1,3,2]);
map=reshape(1:M*N,[M,N]);
d=-(M-1):(N-1);
L=length(d);
result=zeros(2,L);
for ii= 1:L
idx=diag(map,d(ii));
result(:,ii) = sum(T(:,idx),2);
end
2 个评论
Heng
2013-3-10
No, if you were to follow the definition, it would require 2 loops, one over k and one over j.
Also, you could vectorize the 1 loop I've left for you. I just doubt that it's worth it. The primary hard work (the sequence of matrix-vector multiplications) has been vectorized for you.
Image Analyst
2013-3-10
0 个投票
Not sure I understand what you're asking. But yes, there is conv(), as you already know, and there are conv2() and convn() as well, that do convolution in 2 or higher dimensions. You can do "sequences of matrices" if your matrices care constructed correctly and you use the proper function.
Yet another approach and probably the best one, IMO, if you have a long sequence of small matrices. You'll notice that this uses a double for-loop, but the loops are very small since they only run over the dimensions of a single u(:,:,i). This method is also the most memory conservative.
[mu,nu,ku]=size(u);
[mv,nv]=size(v);
L=M+N-1;
result=zeros(nu,L);
for i=1:mu
c=0;
for j=1:nu
t=u(i,j,:);
c = c + conv(t(:).',v(j,:));
end
result(i,:)=c;
end
1 个评论
Even simpler, and only one small loop (in this case nu=2)!
nu=size(u,2);
result=0;
for j=1:nu
t=squeeze(u(:,j,:));
result = result + conv2(t,v(j,:));
end
类别
在 帮助中心 和 File Exchange 中查找有关 Continuous Waveforms 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!