Labfans是一个针对大学生、工程师和科研工作者的技术社区。 | 论坛首页 | 联系我们(Contact Us) |
![]() |
![]() |
#1 |
高级会员
注册日期: 2019-11-21
帖子: 3,006
声望力: 66 ![]() |
![]()
在Matlab中,有没有一种方法可以在同一循环中两次将同一函数与持久变量一起使用?
这个想法是使用卡尔曼滤波器的通用函数(依赖于持久变量)来分别估计两个系统,而无需复制和重命名卡尔曼滤波器函数。一些语言(例如G / LabVIEW)允许定义是否应在其他线程中调用函数;以及因此,复制它的一个实例,可以有效地分离持久变量。但是,Matlab大多仅是单线程的。我希望有一个类似subplot()的命令来激活一个函数的不同实例。 我附上了一个没有用的虚拟示例(我很清楚这可能是向向量添加常数因子的最愚蠢的方式;)) function [i1,i10] = testThredPers() % test function calling a local function with persistent variables twice % number of calles | USER num = 13; % allocate memory i1 = NaN(num,1); i10 = NaN(num,1); % clear persistent variables clear pers % looping for i = 1:num % 1st call of function with persistent variables i1(i) = pers(i,1); % 2nd call of function with persistent variables i10(i) = pers(i,10); end end %% --------- LOCAL FUNCTIONS --------- % function out = pers(x,lcl0) % function with persistent variables persistent lcl if isempty(lcl) lcl = lcl0; end out = x*lcl; end 更多&回答... |
![]() |
![]() |