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 个评论
回答(2 个)
另请参阅
类别
在 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!