Why do I keep getting Nan when I use bsxfun?

2 ビュー (過去 30 日間)
Aditi Bhalerao
Aditi Bhalerao 2017 年 9 月 20 日
コメント済み: Aditi Bhalerao 2017 年 9 月 21 日
Hi, I have two vectors YearAlloc.LoadAlloc of size 1*688 and Gen.Capacity 1*688. The size of YearAlloc.LoadAlloc keeps on changing; i.e the 688 goes on increasing. I want to divide the two vectors to get a vector with one column. I tried
bsxfun(@rdivide,YearAlloc.LoadAlloc.',Gen.Capacity)
It creates Nan where YearAlloc.LoadAlloc is 0. I don't want the Nan. Can you please help me out.

採用された回答

Walter Roberson
Walter Roberson 2017 年 9 月 20 日
0/0 is defined as NaN, but non_zero_value / 0 is defined as sign(non_zero_value)*inf
You are not exactly getting Nan where the LoadAlloc is 0: you are getting it for the combinations where both LoadAlloc and Capacity are 0.
You cannot prevent the NaN except by not dividing 0 by 0. For example,
bsxfun(@rdivide,YearAlloc.LoadAlloc.', max(realmin, Gen.Capacity))
... provided that the Capacity is never negative.
The appropriate way to handle this is to use isnan() afterwards to detect the nans and to assign in whatever value you feel is appropriate for division of 0 by 0, whether you feel the result should be -inf or +inf or 0 or 1 (all of which can be justified.)
  3 件のコメント
Walter Roberson
Walter Roberson 2017 年 9 月 20 日
YearAlloc.AllocPerCapacity( isnan(YearAlloc.AllocPerCapacity) ) = 0;
No loop needed.
Aditi Bhalerao
Aditi Bhalerao 2017 年 9 月 21 日
It worked. Thank you very much for the help.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by