Prod giving a different answer than repeated multiplication

1 vue (au cours des 30 derniers jours)
Divij Gupta
Divij Gupta le 23 Juin 2021
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?
  1 commentaire
Divij Gupta
Divij Gupta le 23 Juin 2021
By 0 as the output I mean the 4x4 zero matrix

Connectez-vous pour commenter.

Réponse acceptée

KSSV
KSSV le 23 Juin 2021
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)
ans = logical
1
isequal(X,Z)
ans = logical
0
  2 commentaires
Divij Gupta
Divij Gupta le 23 Juin 2021
Thank you so much, that makes sense. What function can I use to vectorise matrix multiplication then?
Walter Roberson
Walter Roberson le 23 Juin 2021
Modifié(e) : Walter Roberson le 23 Juin 2021
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)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Multidimensional Arrays 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!

Translated by