1
votes

I'm trying to apply a function I wrote to each element in a matrix using arrayfun, but I'm either using the wrong function or getting my syntax wrong.

I've written a function with 3 inputs, 1 output and saved it to an m file in the same directory. I'd like to call that function, with 1 input being an element in the matrix and the 2 remaining inputs being set, and get an output matrix.

My code looks like:

fun = @(x) my_function(x,input2,input3);
B = arrayfun(@(x) fun, A)

I get the following error

Error using arrayfun function_handle output type is not currently implemented.
1

1 Answers

2
votes

The correct syntax is

B = arrayfun(fun, A);

because fun is already a handle to your anonymous function.

Note that variables input1, input2 have to be defined before you define your function. The values of those variables get "hard-wired" into the function definition. If you later change those variables, that will have no effect on the function.