matlab "FoldList" (generalized cumfn)
4 views (last 30 days)
Show older comments
Darin
on 18 Sep 2020
Commented: Walter Roberson
on 20 Sep 2020
Is there a Matlab equivalent to the Mathematica FoldList function (a generalization of cumsum, cummax, etc., with an arbitrary function).
One could clearly write one, but presumably a built in (such as arrayfun) would be much more efficient. True?
2 Comments
Accepted Answer
Walter Roberson
on 18 Sep 2020
2 Comments
Walter Roberson
on 19 Sep 2020
I see what you mean. No, there is no Mathworks provided function for that.
By the way, for a long time arrayfun was a .m file that looped at the MATLAB level. In those days, arrayfun was faster than a loop only for a very restricted set of functions, and based upon some optimizations that were done for functions but not for scripts. MATLAB improved optimization overall, and arrayfun became no faster than looping... until, that is, arrayfun got converted to a built-in.
More Answers (1)
Darin
on 20 Sep 2020
1 Comment
Walter Roberson
on 20 Sep 2020
Hypothetically, a built-in function for the purpose could build the execution environment for the anonymous function only once, reducing the execution overhead a little. Anonymous functions are slower than regular function (more overhead to invoke them) so it could potentially help a bit.
A "fold" operation that was only emitting a single output (not the "foldList" that you are hoping for, which collects all of the outputs) could potentially also optimize to use in-place update operations. But probably wouldn't do that.
See Also
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!