Using a cell array as an argument of a function handle

Hi all,
Is it possible to use a cell array as arguments in a function handle?
For example:
a(1) = {@(x) x(1) + 2};
a(2) = {@(x) x(2) + 3};
a
a = 1×2 cell array
{@(x)x(1)+2} {@(x)x(2)+3}
b = @(y) y(1) + y(2)
g = function_handle with value:
@(y)y(1)+y(2)
c = b(a)
Operator '+' is not supported for operands of type 'cell'.

Error in solution>@(y)y(1)+y(2) (line 4)
g = @(y) y(1) + y(2)
I would like to get c = a(1) + a(2) or c = @(x) x(1) + 2 + x(2) + 3;
Thanks!

回答(1 个)

a(1) = {@(x) x(1) + 2};
a(2) = {@(x) x(2) + 3};
b = @(x) a{1}(x) + a{2}(x); % merging function handles
my_input = [5, 8]; % for example
c = b(my_input)
c = 18
my_input(1) + 2 + my_input(2) + 3 % to test
ans = 18

3 个评论

Thanks Angelo!
In this simple example your answer works good, but my problem is a more complex one.
I implemented the FORM algorithm using symbolic math and it works perfectly, however I want to use the same code with function handles.
In one part of the original code I have the following lines:
mu = [2e4 12 0.04 2e10 9.82e-4 1e11]; % Mean
sigma = [1.4e3 0.12 4.8e-3 1.2e9 5.9852e-5 6e9]; % Standard deviation
dist = ["normal", "normal", "normal", "normal", "normal","normal"]; % Distribution
numVar = length(mu);
% Using symbolic
x = sym("x", [1 numVar]);
u = sym("u",[1 numVar]);
gx = 0.03 - (x(1) * x(2) ^ 2) / 2 * (3.81 / (x(3) * x(4)) + 1.13 / (x(5) * x(6))); % Limit state equation
for i = 1:numVar
U(i) = mu(i) + sigma(i) * u(i);
end
gy = subs(gx,x,U)
gy = 
Gy = double(subs(gy,u,[0 0 0 0 0 0]))
Gy = 0.0066
I would like to have the same result, but with gx as a function handle. Something like this:
clear
mu = [2e4 12 0.04 2e10 9.82e-4 1e11]; % Mean
sigma = [1.4e3 0.12 4.8e-3 1.2e9 5.9852e-5 6e9]; % Standard deviation
dist = ["normal", "normal", "normal", "normal", "normal","normal"]; % Distribution
numVar = length(mu);
% Using function handle
gx = @(x) 0.03 - (x(1) * x(2) ^ 2) / 2 * (3.81 / (x(3) * x(4)) + 1.13 / (x(5) * x(6))); % Limit state equation
for i = 1:numVar
U(i) = {@(u) mu(i) + sigma(i) * u(i)};
end
U
U = 1×6 cell array
{@(u)mu(i)+sigma(i)*u(i)} {@(u)mu(i)+sigma(i)*u(i)} {@(u)mu(i)+sigma(i)*u(i)} {@(u)mu(i)+sigma(i)*u(i)} {@(u)mu(i)+sigma(i)*u(i)} {@(u)mu(i)+sigma(i)*u(i)}
gy = gx(U)
Operator '.^' is not supported for operands of type 'cell'.

Error in ^ (line 22)
Z = X.^Y;

Error in solution>@(x)0.03-(x(1)*x(2)^2)/2*(3.81/(x(3)*x(4))+1.13/(x(5)*x(6))) (line 22)
gx = @(x) 0.03 - (x(1) * x(2) ^ 2) / 2 * (3.81 / (x(3) * x(4)) + 1.13 / (x(5) * x(6))); % Limit state equation
And class(gy) = 'function_handle', so I could evaluate Gy = gy([0 0 0 0 0 0] = 0.0066
I don't actually understand your intention. Why do you want to make two function handles to input "u"? The code below would work with the same result. This uses function handle but only once. You don't have to put "u" as a input for a function handle. You can use element-wise multiplication (.*).
mu = [2e4 12 0.04 2e10 9.82e-4 1e11]; % Mean
sigma = [1.4e3 0.12 4.8e-3 1.2e9 5.9852e-5 6e9]; % Standard deviation
dist = ["normal", "normal", "normal", "normal", "normal","normal"]; % Distribution
numVar = length(mu);
u = zeros(1, 6);
U = mu + sigma.*u;
gx = @(x) 0.03 - (x(1) * x(2) ^ 2) / 2 * (3.81 / (x(3) * x(4)) + 1.13 / (x(5) * x(6))); % Limit state equation
gx(U)
ans = 0.0066
Actually I need to evaluate the gradient of this:
mu = [2e4 12 0.04 2e10 9.82e-4 1e11]; % Mean
sigma = [1.4e3 0.12 4.8e-3 1.2e9 5.9852e-5 6e9]; % Standard deviation
dist = ["normal", "normal", "normal", "normal", "normal","normal"]; % Distribution
numVar = length(mu);
% Using symbolic
x = sym("x", [1 numVar]);
u = sym("u",[1 numVar]);
gx = 0.03 - (x(1) * x(2) ^ 2) / 2 * (3.81 / (x(3) * x(4)) + 1.13 / (x(5) * x(6))); % Limit state equation
for i = 1:numVar
U(i) = mu(i) + sigma(i) * u(i);
end
gy = subs(gx,x,U);
nablagy = gradient(gy)
nablagy = 
y = zeros(1, 6);
nablaGy = double(subs(nablagy,u,y))
nablaGy = 6×1
-0.0016 -0.0005 0.0008 0.0004 0.0010 0.0010
clear
mu = [2e4 12 0.04 2e10 9.82e-4 1e11]; % Mean
sigma = [1.4e3 0.12 4.8e-3 1.2e9 5.9852e-5 6e9]; % Standard deviation
dist = ["normal", "normal", "normal", "normal", "normal","normal"]; % Distribution
numVar = length(mu);
u = zeros(1, 6);
U = mu + sigma.*u;
gx = @(x) 0.03 - (x(1) * x(2) ^ 2) / 2 * (3.81 / (x(3) * x(4)) + 1.13 / (x(5) * x(6))); % Limit state equation
gx(U)
ans = 0.0066
nablagx = @(x) gradient(gx(x))
nablagx = function_handle with value:
@(x)gradient(gx(x))
nablaGx = nablagx(U)
nablaGx = 0

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Mathematics 的更多信息

产品

版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by