What is the alternative for function tril(n), which is executed without using loops??
Show older comments
%Assume n=50; q=tril(ones(n));
1 Comment
Adam
on 9 Mar 2017
tril doesn't need a loop. Whether it uses one internally or not is a hidden implementation detail.
Answers (1)
Rik
on 9 Mar 2017
0 votes
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 Comment
Adam
on 9 Mar 2017
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++
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!