MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply TodayWrite reduce function, that takes arguments and constantly folds results into next call argument, finally returning a value; i.e. for a binary operator Fb,

reduce(Fb,{a,b,c},start_val) = F(F(F(start_val,a),b),c)

Same can be done for a n-ary operators.

Eg. reduce(@strcat,{'foo','bar','baz','bat'},' ') = reduce(@plus,{1,2,3,4},0) = 10 reduce(@max,{1,2,3,4},{5,6,7,8},5) = 8

Remember, we use cell arrays for passing arguments, not matrices!

10 correct solutions
15 incorrect solutions

Last solution submitted on Jun 18, 2014

1 Comment

Freddy
on 17 Apr 2012

Can't exactly understand why this is shorter than @bmtran's version.

3 Comments