Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fold function in Octave

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?

like image 705
Victor Avatar asked Oct 18 '25 07:10

Victor


1 Answers

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.

like image 141
Warren Weckesser Avatar answered Oct 21 '25 13:10

Warren Weckesser



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!