Defining function handle from class functions with specific values

2 ビュー (過去 30 日間)
johnz05
johnz05 2016 年 12 月 26 日
コメント済み: Steven Lord 2016 年 12 月 26 日
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,

採用された回答

Steven Lord
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 件のコメント
johnz05
johnz05 2016 年 12 月 26 日
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 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 件)

カテゴリ

Help Center および File ExchangeTables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by