Multiplying elements of a matrix and its transpose row-wise

6 views (last 30 days)
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 Comments
Gorbz
Gorbz on 5 Aug 2021
I thought I used the standard * multiplication symbol that yields the dot product.
Matt J
Matt J on 5 Aug 2021
Edited: Matt J on 5 Aug 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?

Sign in to comment.

Answers (2)

KSSV
KSSV on 5 Aug 2021
iwant = sum(A.*A) ;
where A is your 500*2 matrix.

Matt J
Matt J on 5 Aug 2021
result=A.'*A;

Products

Community Treasure Hunt

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

Start Hunting!