Getting NaN using normcdf with array inputs

5 ビュー (過去 30 日間)
Weiqing Xu
Weiqing Xu 2019 年 5 月 3 日
編集済み: Walter Roberson 2019 年 5 月 3 日
I want to get two arrays of data using normcdf, each with its own sigma. So (in both R2019a and R2018a) I tried something like
normcdf((-1:1)',0,[0.5,0.2])
which is supposed to output a 3*2 matrix whose columns contain the data corresponding to the same sigma, right? However, what I got is
>> normcdf((-1:1)',0,[.5,.2])
ans =
0.0228 NaN
0.5000 NaN
NaN NaN
It works perfectly if I just have one parameter being an array. Am I using normcdf in a wrong way? Thank you for any suggestions.

採用された回答

Walter Roberson
Walter Roberson 2019 年 5 月 3 日
If one or more of the input arguments x, mu, and sigma are arrays, then the array sizes must be the same.
Your x is 3 x 1, your sigma is 1 x 2. You are violating the array size compatibility rule.
You need to repmat your sigma to has as many rows as x has.
  2 件のコメント
Weiqing Xu
Weiqing Xu 2019 年 5 月 3 日
Thanks for your answer! But I have a follow-up question, the same instruction can be found in the documentation for normpdf as well, https://www.mathworks.com/help/stats/normpdf.html#d117e604490. However, normpdf does work as what I was expecting.
>> normpdf((-1:1)',0,[.5,.2])
ans =
0.1080 0.0000
0.7979 1.9947
0.1080 0.0000
Any ideas what may cause the difference?
Walter Roberson
Walter Roberson 2019 年 5 月 3 日
編集済み: Walter Roberson 2019 年 5 月 3 日
normcdf handles special cases around sigma == 0. Then it extracts the remaining cases to handle. However, when it extracts the remaining cases, it does so according to the size of sigma, not according to the size of what sigma implicitly expanded with x would be. (Furthermore, the special cases it does for sigma also rely on the size of sigma, not the implicitly expanded size.)
normpdf only boundary checks sigma <= 0, which triggers NaN output. The rest of the calculation happens to be done in a way that supports implicit expansion.

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

その他の回答 (0 件)

カテゴリ

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

タグ

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by