Pointwise multiplication of 3d array with 2d matrix and 1d array and summation by vectorization

6 ビュー (過去 30 日間)
K.E.
K.E. 2016 年 8 月 27 日
編集済み: Stephen Jue 2016 年 8 月 31 日
I have a 2 by 2 matrix
A = rand(2,2)
and a 1 by 5 vector
v = [8 9 12 10 11];
I have a 3d array of dimension 2 by 2 by 5.
Call it D such that
D(:,:,1) = [1 2;3 4];
D(:,:,2) = [5 6;7 8];
D(:,:,3) = [12 11;10 9];
D(:,:,4) = [13 15;17 19];
D(:,:,5) = [21 22;23 28];
How can I do the operations of
J=zeros(2);
K=zeros(2);
for i = 1:5
J = J + D(:,:,i)'*A*D(:,:,i);
K = K + D(:,:,i)'*D(:,:,i);
end
and
Q = zeros(size(D));
for i = 1:5
Q(:,:,i) = v(i)*D(:,:,i);
end
by vectorization in the fastest way. I want to do it because the 3d array very huge dimension in general.

回答 (1 件)

Stephen Jue
Stephen Jue 2016 年 8 月 31 日
編集済み: Stephen Jue 2016 年 8 月 31 日
Hi Jeff,
These operations all require multi-dimensional matrix multiplication, which is not a built-in feature of MATLAB. However, there is a File Exchange function called mtimesx which can be used instead. It makes use of MATLAB's vectorization optimization by calling the "mtimes" function internally. It can do many operations to n-dimensional arrays, for example:
C = mtimesx(A,B) % performs the calculation C = A * B
C = mtimesx(A,'T',B) % performs the calculation C = A.' * B
C = mtimesx(A,B,'g') % performs the calculation C = A * conj(B)
C = mtimesx(A,'c',B,'C') % performs the calculation C = A' * B'
To calculate the sum (J = J + ...), you can use the result from "mtimesx" as an argument to cumsum.
Another alternative if you have the Parallel Computing Toolbox is to use the pagefun function.
I hope that helps.

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by