Multiplying elements of a matrix and its transpose row-wise

8 次查看(过去 30 天)
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 个评论
Gorbz
Gorbz 2021-8-5
I thought I used the standard * multiplication symbol that yields the dot product.
Matt J
Matt J 2021-8-5
编辑:Matt J 2021-8-5
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?

请先登录,再进行评论。

回答(2 个)

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

Matt J
Matt J 2021-8-5

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by