Defining function handle from class functions with specific values
2 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Steven Lord
on 26 Dec 2016
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 Comments
Steven Lord
on 26 Dec 2016
What is the exact full text of the error you receive when you try to either create this anonymous function or call it?
More Answers (0)
See Also
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!