gpt4 book ai didi

function - 指向 MATLAB 函数的指针?

转载 作者:行者123 更新时间:2023-12-02 08:46:21 33 4
gpt4 key购买 nike

所以我在 MATLAB 中有一个 for 循环,其中向量 x 将通过一个函数,比如 cos(x).^2,或者不同的选择,比如 sin(x).^2 + 9.*x。用户将在 for 循环之前选择他想使用的功能。

我的问题是,我不希望循环检查用户在每次迭代中选择的内容。有没有一种方法可以使用指向函数的指针(用户定义的或其他方式),每次迭代都会自动使用?

顺便说一句,这是在脚本中,而不是函数中。

谢谢

最佳答案

您可以使用 function_handles .对于您的示例(使用循环运行所有可用函数):

x = 1:10; % list of input values
functionList = {@(x) cos(x).^2, @(x) sin(x).^2 + 9*x}; % function handle cell-array
for i=1:length(functionList)
functionOut{i} = functionList{i}(x); % output of each function to x
end

关于function - 指向 MATLAB 函数的指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12182558/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com