how to define the factorial if data set is non-integer

12 ビュー (過去 30 日間)
mohd akmal masud
mohd akmal masud 2023 年 9 月 20 日
コメント済み: Dyuman Joshi 2023 年 10 月 16 日
dear all
I have the data set as attached, and the formula as picture below.
I want to calculat the probabilty certain point using the formula as below.
My problem is how to define my factorial, while my data is non-integer.
clc
clear all
close all
%% TO READ DATA SET,
sz = [128 128 64];
fname = 'jaszak18092023tc10n1bckg10mbq10jutaf.a00';
fid = fopen(fname);
data = fread(fid,'*float'); % assuming uint
fclose(fid);
% this is blindly devectorized
% may still be transposed, but i can't tell due to symmetry
% note that data is unit-scale single-precision float
data = reshape(data,sz);
%% TO GET THE MEAN DATA SET
lamda = mean(mean(mean(data)));
% lamda = 0.0223; % lamda REPRESENT mean data set, I got from my data set
e = 2.718; % Euler number (exp)
k = 0.439173; % LET SAY my point to calculate the P (probability) (THE VALUE K FROM MY DATA SET, THE COORDINATE IS , [65 64 1])
j=factorial(k);
P = (lamda^k * exp(-lamda)/factorial(k));
Error using factorial
N must be an array of real non-negative integers.
ANYONE CAN HELP ME?
  11 件のコメント
mohd akmal masud
mohd akmal masud 2023 年 9 月 21 日
Dear @Bruno Luong, you are very correct
Dyuman Joshi
Dyuman Joshi 2023 年 10 月 16 日
Both the answers provided here do not understand nor acknowledge the problem with what OP is trying to do.
The comments above by Bruno and dpb have pointed out what the mistake is.

サインインしてコメントする。

採用された回答

Akshat Dalal
Akshat Dalal 2023 年 10 月 8 日
Hi Akmal,
The factorial operation is only defined for integral values. However, there are extensions to the concept of factorial for non-integer values. One such extension is the gamma function, which can be used to define the factorial for non-integer values. Mathematically,
You can use the inbuilt ‘gamma’ function in MATLAB to calculate factorials of non-integral values.
For more information on the gamma function, please refer the following documentation - https://en.wikipedia.org/wiki/Gamma_function
For more information on using the gamma function in MATLAB, please refer the following documentation - https://in.mathworks.com/help/releases/R2023b/symbolic/gamma.html

その他の回答 (1 件)

Francis Mike John Camogao
Francis Mike John Camogao 2023 年 10 月 14 日
編集済み: Francis Mike John Camogao 2023 年 10 月 14 日
The error you are encountering is because the `factorial` function expects the input `k` to be a non-negative integer, but you are providing it with a floating-point value. To calculate the factorial of a floating-point number, you need to modify the code. You can use the `gamma` function, which calculates the factorial of a real number (including floating-point values). Here's how you can modify your code to fix the error:
clc
clear all
close all
%% TO READ DATA SET,
sz = [128 128 64];
fname = 'jaszak18092023tc10n1bckg10mbq10jutaf.a00';
fid = fopen(fname);
data = fread(fid,'*float'); % assuming uint
fclose(fid);
% this is blindly devectorized
% may still be transposed, but I can't tell due to symmetry
% note that data is unit-scale single-precision float
data = reshape(data, sz);
%% TO GET THE MEAN DATA SET
lamda = mean(mean(mean(data)));
% lamda = 0.0223; % lamda REPRESENT mean data set, I got from my data set
e = 2.718; % Euler number (exp)
k = 0.439173; % LET SAY my point to calculate the P (probability) (THE VALUE K FROM MY DATA SET, THE COORDINATE IS , [65 64 1])
% Calculate factorial using gamma function
j = gamma(k + 1);
P = (lamda^k * exp(-lamda) / j);
% Display the result
fprintf('P = %f\n', P);
I also got and implemented the ideas atop my response.
In this code, I replaced the `factorial` function with the `gamma` function to calculate the factorial of `k`, which is a floating-point number. This should resolve the error you were encountering. I as well modifed, added some lines.

Community Treasure Hunt

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

Start Hunting!

Translated by