Vectorizing a funky matrix multiplication
Mostra commenti meno recenti
I have two matrices H and W, where H is Nx1873 and W is 262144xN (N is a small integer, usually 10-20). I multiply these matrices using the following code:
comp = zeros([512 512 1873 N]);
for i = 1:N
comp(:,:,:,i) = reshape(W(:,i)*H(i,:),[512 512 1873]);
end
Is there a way to vectorize this operation?
1 Commento
Matt J
il 22 Giu 2017
If N is a small integer, vectorization will probably not make much difference.
Also, the operation itself seems like a bad idea. I don't know what you're planning to do with all of those outer products comp(:,:,:,i), but in most situations there are efficient ways to manipulate outer-products without computing them directly. They consume a lot of memory in spite of containing very little information.
Risposte (1)
Wr=reshape(W,[],1,N);
Hr=reshape(H.',1,[],N);
comp=reshape( bsxfun(@times, Wr,Hr), 512,512,[]);
2 Commenti
Matt J
il 22 Giu 2017
If N is a small integer, vectorization will probably not make much difference.
Also, the operation itself seems like a bad idea. I don't know what you're planning to do with all of those outer products comp(:,:,:,i), but in most situations there are efficient ways to manipulate outer-products without computing them directly. They consume a lot of memory in spite of containing very little information.
David Thibodeaux
il 22 Giu 2017
Categorie
Scopri di più su Creating and Concatenating Matrices in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!