The Matlab fold function is a very useful functional programming construct. Unfortunatlely, fold is not available io Cody players.
In this problem we are required to create the function foldPoly, that takes in 2 inputs, a polynomial vector P and a values vector V. Assuming V is never empty, we define the function as follows:
foldPoly = @(P,V) fold(@(a,b) polyval(P,a+b),V);
which is equivalent, in older Matlab, to:
function z = foldPoly(P,V)
z = V(1);
for i = 2:length(V)
z = polyval(P,(V(i)+z));
end
end
The challenge here is to write the foldPoly function in just one (1) line of code, excluding the function start and end line.
-----------------
NOTE: Empty lines and comment lines (starting with %) shall not be counted, but semicolons (;) will be considered as an end-of-line character.
HINT: It is helpful to have a Matlab in your computer (or you can use Matlab Online), to be able check if your implementation behaves the same way as the built-in fold function.
Solution Stats
Problem Comments
2 Comments
Solution Comments
Show comments
Loading...
Problem Recent Solvers4
Suggested Problems
-
Determine whether a vector is monotonically increasing
23712 Solvers
-
Find all elements less than 0 or greater than 10 and replace them with NaN
15825 Solvers
-
196 Solvers
-
Replace multiples of 5 with NaN
472 Solvers
-
Remove All elements less than 5
146 Solvers
More from this Author116
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
A slight correction, Ramon - The function fold() is not available as it is a part of a Toolbox, and Cody doesn't have access to Toolboxes.
Cody is updated with the latest MATLAB release, as you can check by running the command "ver" on scratch pad.
The problem description is updated... Thanks Dyuman.