Defining function handle from class functions with specific values

1 Ansicht (letzte 30 Tage)
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,

Akzeptierte Antwort

Steven Lord
Steven Lord am 26 Dez. 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 Kommentare
johnz05
johnz05 am 26 Dez. 2016
Thanks Steve, that's of great help but now I can't figure out why this is working and not
pdf = @(x,beta,ZB) CustomPL(1000,ZB,beta).pdf(x)
as my class definition does not seem to fall in the cases where the dot notation and function notation would work differently. Any idea why? Many thanks. Best
Steven Lord
Steven Lord am 26 Dez. 2016
What is the exact full text of the error you receive when you try to either create this anonymous function or call it?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Tables finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by