Effacer les filtres
Effacer les filtres

Multiplying elements of a matrix and its transpose row-wise

8 vues (au cours des 30 derniers jours)
Gorbz
Gorbz le 5 Août 2021
Modifié(e) : Matt J le 5 Août 2021
I am a novice in Matlab. I have a variable U of the class 'double' and size 500 2 (so it corresponds to a 500x2 matrix). Each row corresponds to a vector so in reality I have 500 vectors. I want to take the dot product of each vector with itself and add the results:
result = U(1)'*U(1) + U(2)'*U(2) + ... + U(500)'*U(500)
I cannot figure out how to do this.
EDIT:
For example, if U = [1 2; 3 4] then I can define result1 = dot(U(1,:)',U(1,:)) and result2 = dot(A(2,:)',A(2,:))
Then I need to do result1 + result2. So I need to store the dot product of each row of U with it self to a list and then add the elements of the list.
Unfortunately I cannot make the following loop work:
for i = 1:length(A)
result1 = dot(A(i,:)',A(i,:))
end
ANSWER:
y = 0;
for n = 1:length(U)
y = y + dot(A(U,:)',A(U,:))
end
  3 commentaires
Gorbz
Gorbz le 5 Août 2021
I thought I used the standard * multiplication symbol that yields the dot product.
Matt J
Matt J le 5 Août 2021
Modifié(e) : Matt J le 5 Août 2021
No, the * multiplication symbol in Matlab means matrix multiplication and U' means the conjugate tranpose of U. Is your intended product operation supposed to produce a 2x2 output or something else?

Connectez-vous pour commenter.

Réponses (2)

KSSV
KSSV le 5 Août 2021
iwant = sum(A.*A) ;
where A is your 500*2 matrix.
  2 commentaires
Gorbz
Gorbz le 5 Août 2021
The result of this is a 2d vector I am afraid.
Stephen23
Stephen23 le 5 Août 2021
sum(A.*A,'all')

Connectez-vous pour commenter.


Matt J
Matt J le 5 Août 2021

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by