Multiplying all 2D square matricies in a 3D matrix

3 ビュー (過去 30 日間)
Connor LeClaire
Connor LeClaire 2021 年 10 月 21 日
コメント済み: Paul 2021 年 10 月 21 日
Consider a 3D matrix 'T' made from a concatenation of N square matrices of size m. Is there a easy way to multiply them all together (m1 x m2 x m3 x m4 ... x mN) into a single 2D matrix 'total' of size m? Similar to something like;
total = prod(T(:,:,:));
%This returns a 3D matrix of each 2D matrix of size (1,m)
In a for loop it may look like:
total = eye(m);
for i = 1;N
total = total*T(:,:,i);
end
Is it also possible to stop the multiplication at somepoint along the 3D matrix at point 1 < h < N;
total = prod(T(:,:,1:h))
I should also mention in my specific application the matrices are symbolic with both variables and numbers.
  2 件のコメント
James Tursa
James Tursa 2021 年 10 月 21 日
編集済み: James Tursa 2021 年 10 月 21 日
Just use the for-loop. If for some reason you were really pressed for speed you could write a mex routine that could avoid all the intermediate data copies. But even this might only save you 10% - 20% execution time.
Paul
Paul 2021 年 10 月 21 日
Can mex routines work with symbolic variables?

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

採用された回答

Paul
Paul 2021 年 10 月 21 日
編集済み: Paul 2021 年 10 月 21 日
Easier than putting that loop in a function?
function mprod = matprod(T,h)
% should do some input checking to verify that 1 <= h <= size(T,3)
mprod = T(:,:,1);
for ii = 2:h
mprod = mprod*T(:,:,ii);
end
end
  3 件のコメント
Paul
Paul 2021 年 10 月 21 日
I'm not aware of one, but maybe someone else has what you're loooking for. If they do, I really want to see it.
Connor LeClaire
Connor LeClaire 2021 年 10 月 21 日
Guess I could write a function and pass the 3D matrix and the indexes to start and stop but meh

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by