Does MATLAB has matrix convolution function
Afficher commentaires plus anciens
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!
Réponse acceptée
Plus de réponses (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 commentaires
Heng
le 10 Mar 2013
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
le 10 Mar 2013
0 votes
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 commentaire
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
Catégories
En savoir plus sur Continuous Waveforms dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!