matlab "FoldList" (generalized cumfn)

3 ビュー (過去 30 日間)
Darin
Darin 2020 年 9 月 18 日
コメント済み: Walter Roberson 2020 年 9 月 20 日
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 件のコメント
madhan ravi
madhan ravi 2020 年 9 月 18 日
Could you provide an example?
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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by