Vector as Function Input

조회 수: 14 (최근 30일)
Cory Ibanez
Cory Ibanez 2019년 2월 19일
댓글: Cory Ibanez 2019년 2월 19일
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

답변 (1개)

madhan ravi
madhan ravi 2019년 2월 19일
편집: madhan ravi 2019년 2월 19일
% 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
madhan ravi
madhan ravi 2019년 2월 19일
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 2019년 2월 19일
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!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by