Using a cell array as an argument of a function handle
조회 수: 5 (최근 30일)
이전 댓글 표시
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
b = @(y) y(1) + y(2)
c = b(a)
I would like to get c = a(1) + a(2) or c = @(x) x(1) + 2 + x(2) + 3;
Thanks!
댓글 수: 0
답변 (1개)
Angelo Yeo
2023년 7월 19일
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)
my_input(1) + 2 + my_input(2) + 3 % to test
댓글 수: 3
Angelo Yeo
2023년 7월 20일
편집: Angelo Yeo
2023년 7월 20일
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)
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Object Properties에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!