Prod giving a different answer than repeated multiplication
1 回表示 (過去 30 日間)
古いコメントを表示
X(:,:,1) = [0.0000 + 0.0000i 0.0000 + 0.0000i 1.0000 + 0.0000i 0.0000 + 0.0000i;0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 1.0000 + 0.0000i;1.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i;0.0000 + 0.0000i 1.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i];
X(:,:,2) = [0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 - 1.0000i 0.0000 + 0.0000i;0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 - 1.0000i;0.0000 + 1.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i;0.0000 + 0.0000i 0.0000 + 1.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i];
X(:,:,3) = [0.0000 + 0.0000i 1.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i;1.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i;0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i -1.0000 + 0.0000i;0.0000 + 0.0000i 0.0000 + 0.0000i -1.0000 + 0.0000i 0.0000 + 0.0000i];
X(:,:,4) = [0.0000 + 0.0000i 0.0000 - 1.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i;0.0000 + 1.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i;0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 1.0000i;0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 - 1.0000i 0.0000 + 0.0000i];
I have the given 3D array. For some reason prod(X(:,:,1:4),3) gives 0 as the output. That does not match with the output for X(:,:,1)*...X(:,:,4). I do not want to use a loop, I want to use a vectorised method. However, prod is giving me issues. Am I doing something wrong? Is there another method?
採用された回答
KSSV
2021 年 6 月 23 日
prod gives you element by element multiplication.
Check:
A = rand(2,2,3) ;
X = prod(A,3) ;
Y = A(:,:,1).*A(:,:,2).*A(:,:,3) ; % element by element mulltiplication
Z = A(:,:,1)*A(:,:,2)*A(:,:,3) ;
isequal(X,Y)
isequal(X,Z)
2 件のコメント
Walter Roberson
2021 年 6 月 23 日
編集済み: Walter Roberson
2021 年 6 月 23 日
You cannot vectorize matrix multiplication.
You can make the process a little more compact by using fold() https://www.mathworks.com/help/symbolic/fold.html . However that requires the Symbolic Toolbox
The elements you would implicitly loop over would be created by using num2cell(A,3)
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!