Multiplying elements of a matrix and its transpose row-wise
6 views (last 30 days)
Show older comments
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
Answers (2)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!