I got the error of incorrect argument data type or missing argument in call to function 'gamma'.

2 views (last 30 days)
function z=dist(x,rd,gm)
arguments
x (1,:) {mustBeInteger}
rd (1,:) {mustBePositive}
gm (1,:) {mustBePositive}
z= (rd.^x)*gamma(gm)./gamma(gm+x).*(hypergeom(1+x,gm+x,-rd));
end
function a = rdist(n,rd,gm)
u=rand(1,n);
x=int16(n);
for i=1:n
a=dist(x(i),rd(i),gm(i));
while a(i)<u(i)
x(i)=x(i)+1;
a=a+dist(x(i),rd(i),gm(i));
end
end

Answers (1)

Steven Lord
Steven Lord on 23 Aug 2022
The gamma function in MATLAB requires its input to be a floating-point number, specifically the input must be "Data Types: single | double"
answer = gamma(5)
answer = 24
If you try to call it on with a value of an integer type as input, you'll receive an error. The result of adding a double and an integer is of the integer type, as you can see by looking at the information for the result variable in the output of whos below.
d = 5;
in = int8(1);
result = d + in;
whos d in result
Name Size Bytes Class Attributes d 1x1 8 double in 1x1 1 int8 result 1x1 1 int8
Calling double on result and calling gamma on that converted value will work, but calling gamma on result itself will not.
gamma(double(result))
ans = 120
gamma(result) % Will error
Check for incorrect argument data type or missing argument in call to function 'gamma'.
How is this relevant to your function? x must be of an integer type, so gm+x must be of that same integer type, and you try computing gamma(gm+x) in your code.
  1 Comment
Kee Wah Fo
Kee Wah Fo on 26 Aug 2022
x is integer, a discrete number of a probability distribution. But gm is a positive number. Isn't my code error?
>> gm=1.2;
>> x=2;
>> gamma(gm+x)
ans = 2.4240
I am trying to generated a random data from a new distribution, due to new distribution is not include in Mathlab. Is that have other methods to do? Thank you.

Sign in to comment.

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!