1

I want to use a vector input such of dimension [m;1] in a function that takes in m number of inputs. For example:

syms x1 x2;

f = x1^2 + x2^2;
F = matlabFunction(f);
x = [1;1];
F(x);

The above code does not work because F is a function of 2 inputs and it only sees the vector x as a single input. I know I can say F(x(1),x(2)) and the above would work but I want it to work for a function of m variables and a vector of m length.

All help is appreciated. Thanks.

1

1 Answer 1

3

You will want to first convert x to a cell and then pass it with {:}.

xcell = num2cell(x);
F(xcell{:});

Alternately, you can specify that you want x1 and x2 to be passed as an array when you call matlabFunction using the Vars parameter.

F = matlabFunction(f, 'Vars', {[x1, x2]});
F([1 1]);
Sign up to request clarification or add additional context in comments.

2 Comments

The top one worked beautifully. Thanks for the help and the quick response.
@ktobrien if this worked for you please consider marking it as a solution to help anyone with a similar question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.