Sum of matrix times scalars

1 回表示 (過去 30 日間)
Mohammed Kagalwala
Mohammed Kagalwala 2019 年 10 月 21 日
編集済み: Mohammed Kagalwala 2019 年 10 月 21 日
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 !
  3 件のコメント
Stephen23
Stephen23 2019 年 10 月 21 日
Mohammed Kagalwala
Mohammed Kagalwala 2019 年 10 月 21 日
They are in different individual variable names however no reason they can't be grouped in a cell or 3D matrix

サインインしてコメントする。

採用された回答

Matt J
Matt J 2019 年 10 月 21 日
編集済み: Matt J 2019 年 10 月 21 日
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 件のコメント
Guillaume
Guillaume 2019 年 10 月 21 日
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);
Mohammed Kagalwala
Mohammed Kagalwala 2019 年 10 月 21 日
編集済み: Mohammed Kagalwala 2019 年 10 月 21 日
Thank you for your help ! The reshape solutions both work, however on average Matt's solution is faster ( I tested with N = 10000). My other method was coming up with an S1 and S2 such that T*S1*q*S2 = to the 3x3 sum I want. Here T is 3x3N, and q is Nx1. I'm going to try and extend the reshape idea to the following double sum, , where = 3x3 matrix.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLinear Algebra についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by