Sum of matrix times scalars
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Mohammed Kagalwala
le 21 Oct 2019
Modifié(e) : Mohammed Kagalwala
le 21 Oct 2019
Hi,
I have a series of 3x3 matrices T1, T2, T3 ... TN. In addition, I have a series of scalars a1, a2, ... aN. I wish to perform the following sum WITHOUT the use of a for-loop. T1*a1 + T2*a2 + .... TN*aN. I have solved this using a for loop currently, but wish to take advantage of Matlabs superior data structures i.e. 3D arrays, cells, etc.
Thank you for your help !
Réponse acceptée
Matt J
le 21 Oct 2019
Modifié(e) : Matt J
le 21 Oct 2019
Hold your 3x3 matrices in a 3x3xN array caled T and your scalars in an Nx1 vector called a and do,
result=reshape(T,9,[])*a(:);
result=reshape(result,3,3);
2 commentaires
Guillaume
le 21 Oct 2019
Note that if a is a Nx1 vector, then a(:) and a are the same.
Another option is:
result = sum(T .* reshape(a, 1, 1, []), 3);
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Linear Algebra 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!