Jack Elsey
Jack Elsey

Reputation: 134

taking function with vector argument and defining new anonymous function with arbitrary subset of vector members as arguments

I have a function vector_function that takes a 1 by 6 vector variable as its only argument. I would like to define an anonymous function anon_fun based on vector_function that specifies certain members of the 1 by 6 vector as arguments and assigns default values to the rest.

For example, if I wanted to create anon_fun using the first three members of the 1 by 6 vector variable as input arguments, I could write:

% Define default variable values.
var_def = [1,2,3,4,5,6];

% Set which variables to use as arguments.
var_flag = [true,true,true,false,false,false];

% Define anonymous function that takes 
anon_fun = @(var)...
vector_function([var(1),var(2),var(3),var_def(4),var_def(5),var_def(6)]);

Is there a way to handle all 2^6 = 64 possible permutations of var_flag? I could change the way vector_function takes input, but that will require significant recoding.

Upvotes: 3

Views: 184

Answers (1)

m7913d
m7913d

Reputation: 11064

You can use the following definition for anon_fun:

anon_fun = @(var) vector_function(var .* var_flag + var_def .* ~var_flag);

Upvotes: 4

Related Questions