how to connect different classes?
2 次查看(过去 30 天)
显示 更早的评论
Dear all, Suppose I have the following function
function varargout=myfunc(f,varargin)
atmp=cell(500,4);
iter=0;
nf=numel(f);
fval=nan(nf,1);
for ii=1:nf
fval(ii)=f{ii}(varargin{:});
end
function c=plus_(a,b)
id=find(strcmp(atmp(:,1),'+') && [atmp{:,2}]==a && [atmp{:,3}]==b);
if isempty(id)
c=a+b;
iter=iter+1;
atmp(iter,:)={'+',a,b,c};
else
c=atmp{id,end};
end
end
end
This function would not work, but it serves to illustrate the question I would like to ask. The point with this function is that it has a nested function "plus_" and the variables created in the main function (atmp, iter, etc.) are also available to the nested function and the nested function can modify those variables.
In this simple example, "f" is just an array of function handles, e.g. f{1}=@(x,y)exp(x+2*y); the nested function merely adds a and b, but does the actual calculation if a+b has not been computed before. If a+b has been computed before, then the result is just fetched.
For this to work, it should be written in classes overloading operations +, -, *, /, etc. But then how to implement variables atmp and iter such that they are available to the various methods? One solution would be to define atmp and iter as global variables to each of the overloaded methods and initialize it outside the class. A better solution, perhaps, would use handles, notify, events, addlistener, etc. but I do not know how to implement them. Any ideas?
Thanks, P.
5 个评论
Adam
2014-8-18
I'm confused what the question is here. Your title and the actual part of your post with a ? talks about classes yet everything else seems to be related to nested functions.
If you have a class that overloads the + operator and any other operators then properties of the class are available to the overloaded operators in the nature of being a class.
回答(1 个)
Aurele Turnes
2014-8-4
编辑:Aurele Turnes
2014-8-4
Like per isakson pointed out, I think that you can achieve this by using memoization techniques. The following page on MATLAB Central gives a pretty good example of how to do this:
In order to keep the same types of variable that you are using, your function could look something like this:
function f = memoize()
%one-arg F, inputs testable with ==
%allow nonscalar input.
atmp = cell(500,4);
iter = 0;
f = @inner;
function out = inner(a,b,F)
% find which in's already computed in x
if iter>0
ind= strcmp(atmp(1:iter,1),'+') & (cell2mat(atmp(1:iter,2))==a) & (cell2mat(atmp(1:iter,3))==b);
if any(ind)
out = atmp{ind,4};
else
out = eval(['a' F 'b']);
iter = iter+1;
atmp(iter,:) = {F, a , b, out};
end
else
iter = iter+1;
out = eval(['a' F 'b']);
atmp(iter,:) = {F, a , b, out};
end
end
end
You can then call it and use it as follows:
f = memoize
f(3,4,'+')
f(3,5,'+')
f(3,4,'+')
f(3,4,'*')
Look at the example function memoize2 in the link above to vectorize this technique.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Performance and Memory 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!