Defining function handle from class functions with specific values

조회 수: 3 (최근 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 CenterFile Exchange에서 Tables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by