How to improve a for
1 回表示 (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
採用された回答
その他の回答 (2 件)
Jan
2014 年 10 月 11 日
The vectorization is better, but here is a cleaner and more efficient loop method:
f = zeros(10, 1); % pre-allocate!!!
for k = 1:10
f(k, 1) = -(Q(:,k)' * Q(:,k));
end
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!