Fold function in Octave

荒凉一梦 提交于 2019-12-10 19:27:55

问题


Is there standard implementation of fold (reduce, aggregate etc) for one dimensional vector in Octave? If no, is there any way to express fold without using a loop statement?


回答1:


The miscellaneous package provides the function reduce. For example,

octave:6> reduce(@(x,y)(x*y), [1:5])
ans =  120

If you look at the source code for reduce, you'll see that it is a fairly simple Octave function that is implemented with a for loop, so it won't be more efficient than implementing the reduction with a for loop of your own.



来源:https://stackoverflow.com/questions/22898711/fold-function-in-octave

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!