Vector as Function Input

8 visualizaciones (últimos 30 días)
Cory Ibanez
Cory Ibanez el 19 de Feb. de 2019
Comentada: Cory Ibanez el 19 de Feb. de 2019
I want to find a way to pass the inputs to a function as a vector instead of having to specify the individual inputs to the function. I want to do this because I have another function that takes in a function handle as its input as well the input values to that function to generate a set of sigma points(I'm writing a ukf). It looks a bit like this(very over-simplified
function [x]=myFunc(func_handle,func_input)
x = func_handle(func_input)
end
Now the issue is in my implementation of the func_handle, if I declare a function as follows, I have no issues.
test_func = @(x)[x(1) + x(2); x(2)^2; x(1)^2 + x(3)^2];
But for my application, my function is very long and complex and can't be written in a single line as such so I have it written in terms of symbolic expressions. But when written as such, I can't get it to work right. I could use some help with being able to define the function such that the input values can be given as a vector so it can be used in the above function properly.
%% Function Test
clear all
close all
syms a b c
test1 = b+a;
test2 = b^2;
test3 = a^2 + c^2;
test = [test1;test2;test3];
test_func = @(input)[test1;test2;test3];
test_input = [1;2;3];
test_func(test_input) %This doesn't work
test_func = matlabFunction(test);
test_func(test_input(1),test_input(2),test_input(3)) %This works but input can only be one size

Respuestas (1)

madhan ravi
madhan ravi el 19 de Feb. de 2019
Editada: madhan ravi el 19 de Feb. de 2019
% Function Test
syms a b c
test1 = b+a;
test2 = b^2;
test3 = a^2 + c^2;
test = [test1;test2;test3];
test_input = num2cell([1;2;3]);
test_func = matlabFunction(test);
test_func(test_input{:}) % have a look here
  3 comentarios
madhan ravi
madhan ravi el 19 de Feb. de 2019
num2cell() converts it into a cell array ,
"Without changing the actual defining of the variable in the first place."
I have no idea whatever that you mean by that.
Cory Ibanez
Cory Ibanez el 19 de Feb. de 2019
I wasn't sure if num2cell would only apply in the actual defining of the input values. I tested it out and I now see I can do:
test_input = [1;2;3];
test_input = num2cell(test_input);
This should solve my issue thanks!

Iniciar sesión para comentar.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by