Vector as Function Input
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Antworten (1)
madhan ravi
am 19 Feb. 2019
Bearbeitet: madhan ravi
am 19 Feb. 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 Kommentare
madhan ravi
am 19 Feb. 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.
Siehe auch
Kategorien
Mehr zu Symbolic Math Toolbox finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!