How to improve a for
Show older comments
Hello, i want to do this operation:
f = -[Q(:,1)'*Q(:,1); Q(:,2)'*Q(:,2); Q(:,3)'*Q(:,3); Q(:,4)'*Q(:,4); Q(:,5)'*Q(:,5); Q(:,6)'*Q(:,6); Q(:,7)'*Q(:,7); Q(:,8)'*Q(:,8); Q(:,9)'*Q(:,9); Q(:,10)'*Q(:,10)];
i have thought to do this with a for
n=10;
i=1;
while i<n+1
f(i,1)=-[Q(:,i)'*Q(:,i)]; %save in columns
i=i+1;
end
but i think there must be some way to vectorize this easily. Could you help me?
Thank you.
Accepted Answer
More Answers (2)
Andrei Bobrov
on 11 Oct 2014
Edited: Andrei Bobrov
on 11 Oct 2014
f = -dot(Q,Q).'
Categories
Find more on Loops and Conditional Statements 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!