Defining function handle from class functions with specific values
2 次查看(过去 30 天)
显示 更早的评论
Hello all and happy holidays,
I have created a class CustomPL defining a discrete Power Law with parameters beta, ZB and the max number of discrete values. Inside this class, I have created a pdf function depending only on x.
Now I am trying to create a function handle from the main script depending only on beta, ZB and x, but I have not found yet a way to make it work.
Code is as follows:
classdef CustomPL<handle
properties
max
ZB
beta
norm
end
methods
function self = CustomPL(max, ZB, beta)
self.max = max;
self.ZB = ZB;
self.beta = beta;
self.norm = 0;
for i=1:max
self.norm = self.norm + (1/i^beta);
end
self.norm = self.norm / (1-ZB);
end
function pdf = pdf(self,x)
assert(floor(x)==x&&x<=self.max);
if x==0
pdf = self.ZB;
else
pdf = (1/x^self.beta)/self.norm;
end
end
end
end
Now I am trying to create a function handle pdf = @(x,beta,ZB) CustomPL(1000,ZB,beta).pdf(x) but this does not seem to work and I have not found a way around yet. Would you have any idea to get around that?
Many thanks in advance,
Best,
0 个评论
采纳的回答
Steven Lord
2016-12-26
One easy way is to use function notation.
pdf = @(x,beta,ZB) pdf(CustomPL(1000,ZB,beta), x)
You can invoke a method either using dot notation, like object.methodname(...), or using function notation like methodname(object, ...). In almost all circumstances, those will do the same thing. [The documentation gives a description of a circumstance where they won't, but those probably won't apply in this case unless x is an object with certain characteristics or you've overloaded indexing for your object.]
2 个评论
Steven Lord
2016-12-26
What is the exact full text of the error you receive when you try to either create this anonymous function or call it?
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!