Calculate only diagonal elements of multidimensional array product
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Michael Werther
le 28 Nov 2019
Commenté : Michael Werther
le 13 Déc 2019
Hi everybody,
I have two arrays and , where M is small and N is large. What would be the fastest way to calculate for all l? I could do
sum(repmat(A.',[1,1,N]).*B,1), but since N is large this doesn't seem the best idea to me. Any help is appreciated.
Thanks
3 commentaires
David Goodmanson
le 2 Déc 2019
Hi Michael, your question was pretty clear since it's only a single sum instead of a double one, so my comment is toast.
Réponse acceptée
Matt J
le 2 Déc 2019
Modifié(e) : Matt J
le 2 Déc 2019
Assuming your Matlab version is post-R2016b
reuslt = sum(B.*A.',1)
Otherwise, assuming your Matlab version is post-R2008
result = sum(bsxfun(@times, B,A.'),1)
And even if your Matlab version is really, really, really old, then there is still as a last resort,
At=A.';
C=diag(At(:))*reshape(B,[],N));
result=sum(reshape(C,M,M,N),1)
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!