What is the alternative for function tril(n), which is executed without using loops??
显示 更早的评论
%Assume n=50; q=tril(ones(n));
1 个评论
Adam
2017-3-9
tril doesn't need a loop. Whether it uses one internally or not is a hidden implementation detail.
回答(1 个)
Rik
2017-3-9
0 个投票
Without any context for your use, it is impossible to suggest methods to vectorize your code. As Adam mentioned, tril itself does not inherently need a loop. If you need to loop through q=tril(ones(n)) and do things with it, you can generally speed up that loop with cellfun.
1 个评论
Adam
2017-3-9
cellfun is actually slower than an equivalent for loop in most cases, though where it is important a proper speed test using timeit is useful. I like cellfun and arrayfun for code neatness (though some find a for loop neater), but for speed unfortunately they are not fast unlike using boost or std for_each or transform operations in C++
类别
在 帮助中心 和 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!