Why is @(x) used in matlab and how does it work?

조회 수: 569 (최근 30일)
Alekhya
Alekhya 2020년 5월 11일
편집: madhan ravi 2020년 5월 11일
pdfx = @(x) 2*sqrt(x); % Define PDF
x = rand(1,100000); % Generate Random ‘x’
px = pdfx(x); % Generate Data
This is a piece of code used to generate a random variable of self defined pdf. Why is @(x) used here and how is it working.

채택된 답변

KALYAN ACHARJYA
KALYAN ACHARJYA 2020년 5월 11일
편집: KALYAN ACHARJYA 2020년 5월 11일
Its creates the function handle, I suggest ti go through the following MATLAB Documentation, and any issue let us know here
1.See the example, the first line cretes the function named as "pdfx" (Function is 2*sqrt(x))
pdfx = @(x) 2*sqrt(x);
2. Next you have defined x some random data
x = rand(1,100000); % Generate Random ‘x’
3. Passing the x values in the function pdfx as defined
px = pdfx(x);
Its similar to, defined the function file
function y=pdfx(x)
y =2*sqrt(x);
end
Now call the function
x = rand(1,100000);
px = pdfx(x);
Please go through the above mentioned link, you will get more information
Experts any comments please (If I missed any important issue)

추가 답변 (1개)

madhan ravi
madhan ravi 2020년 5월 11일
편집: madhan ravi 2020년 5월 11일
It’s just a matter of choice as Kalyan mentioned above how it works. The usage depends completely on the coder. As pdfx(...) was defined before generation of data , it was defined as a function handle here. It’s equivalent to pdfx = ... without @(x) after generation of x.
  댓글 수: 3
Alekhya
Alekhya 2020년 5월 11일
편집: Alekhya 2020년 5월 11일
Its not equivalent to pdfx(...) without @(x) after generation of x.
>> x = rand(1,100000);
px = pdfx(2*sqrt(x));
It is giving an error "Array indices must be positive integers or logical values"
madhan ravi
madhan ravi 2020년 5월 11일
I meant to say
pdfx = 2*sqrt(x)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Geometry and Mesh에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by