matlab "FoldList" (generalized cumfn)

3 次查看(过去 30 天)
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 个评论
Darin
Darin 2020-9-19
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

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2020-9-18
  2 个评论
Darin
Darin 2020-9-19
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 2020-9-19
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.

请先登录,再进行评论。

更多回答(1 个)

Darin
Darin 2020-9-20
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 个评论
Walter Roberson
Walter Roberson 2020-9-20
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by