Problem 651. Functional inputs
Return an n-dimensional array whose elements are the results of a function over a set of vectors.
The (i,j,k, ...n)th element of the output matrix is the results of executing the function on the ith element of the first vector, jth element of the second, and so forth. The function will be passed in, accepts n arguments, and returns a single number.
For example, if the function is:
minus12 = @(a) a(1)-a(2);
and the inputs are:
as = [1 3 5 7]; bs = [0 1 2 3 4];
then the output would be:
>> gridit(minus12, as, bs) ans = 1 0 -1 -2 -3 3 2 1 0 -1 5 4 3 2 1 7 6 5 4 3
Solution Stats
Problem Comments
-
2 Comments
I'd like to include the test case listed below, but I'm stumped as to why it doesn't work:
%%
x1 = [ 1 2 3 ]
x2 = [ 10 20 ]
y_correct = [9 19; 8 18; 7 17]
y_actual = gridit(@minus, x1, x2);
assert(isequal(y_actual, y_correct));
Alex, I believe it is because the 'minus' function requires two input arguments. The test cases and the example use functions that accept a single input argument. Hope that helps.
Solution Comments
Show commentsProblem Recent Solvers18
Suggested Problems
-
chance in percent for minimum K heads when a good coin is tossed N times?
51 Solvers
-
2182 Solvers
-
Back to basics 13 - Input variables
266 Solvers
-
Sum of first n terms of a harmonic progression
419 Solvers
-
Find the maximum number of decimal places in a set of numbers
2854 Solvers
More from this Author3
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!