Clear Filters
Clear Filters

matlab "FoldList" (generalized cumfn)

4 views (last 30 days)
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
madhan ravi
madhan ravi on 18 Sep 2020
Could you provide an example?
Darin
Darin on 19 Sep 2020
Good point:
cumfun(fn,V) == [V(1), fn(V(1),V(2)), fn(fn(V(1),V(2),V(3)),...]
so that
cumfun(@sum,V) = cumsum(V),
cumfun(@max,V) = cummax(V), ...
I also use a three element form,
cumfun(fn,S,V) where S replaces V(1) in the output.
This form is a bit irrelevant for sum, max, min, etc... but is useful when fn does not have an obsvious result with a single scalar input.
It's easy to code in Matlab... but I'd prefer to avoid custom code if there's a faster and more convenient built-in.
Thanks

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 18 Sep 2020
  2 Comments
Darin
Darin on 19 Sep 2020
Thanks, but I believe that would be an equivalent to Mathematica's Fold, not to FoldList, yes?
It would output the last element of the FoldList vector result, rather than the entire result vector... just like sum(V) outputs the last element of the result from cumsum(V)
I was looking for an equivelent to sumsum, cummax, etc. that can work with an arbitrary function (rather than sum, max, ..where the function implicitly has a scalar output when the overall output is a vector vs. a list, sort of like arrayfun without the 'uniformoutput' option).
This could obviously be written as a simple loop: the presumption was that a built-in would be much faster, as with arrayfun. True?
I'm also a bit confused over why "Fold" is in the symbolic math toolbox... A bit costly for simple numerical work?
Thanks anyway
Walter Roberson
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.

Sign in to comment.

More Answers (1)

Darin
Darin on 20 Sep 2020
Thanks, Walter, for the info and the history lesson!
I'm not sure how well this one could be sped up anyway- since the generalized feedback of non-associative functions defeats parallelism. .m file it is, then! Thanks for letting me know I'm not re-inventing the wheel... again. ;-)
  1 Comment
Walter Roberson
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.

Sign in to comment.

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!