Avoiding NaN Values When Using a Loop

I'm trying to find entropy from a Probability Mass Function (PMF) using the following equation:
Where is a PMF value and m is the amount of values of the PMF.
From there I created this code:
entropy = 0;
pmf = [0.1, 0.015, .. , 0.02, 0.1];
% Summation
for l = 1:length(pmf)
entropy = entropy + pmf(l) * log2(pmf(l));
fprintf('Entropy Val %d: %f.20 \n', l, entropy)
end
entropy = entropy * -1;
But the entropy value started to give out a NaN value when . Is there a way to avoid that?

 採用された回答

Matt J
Matt J 2022 年 3 月 22 日
編集済み: Matt J 2022 年 3 月 22 日

0 投票

entropy=-cumsum(pmf.*log2(pmf),'omitnan');
fprintf('Entropy Val %d: %f.20 \n', l:numel(entropy), entropy)

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeEntering Commands についてさらに検索

タグ

質問済み:

2022 年 3 月 22 日

編集済み:

2022 年 3 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by