norm calculation using for loop
7 次查看(过去 30 天)
显示 更早的评论
采纳的回答
Adam
2016-8-12
n = norm( x );
does it without a loop. There is no reason to do it with a loop. Even if you don't use norm you would still vectorise is as:
n = sqrt( sum( x.^2 ) );
If you really want a loop the derivation is not difficult though. The vectorised version should certainly be faster, but you can test this yourself.
4 个评论
KL
2017-10-16
As Adam has mentioned in his answer, loop is not necessary but if it's a homework and you must use a loop, then read the documentation to know how it works and then use the for loop iterator to index through the vector.
更多回答(0 个)
另请参阅
类别
在 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!