How can I do this summation formula in Matlab? The value of N = 0:2:32; Let P = (factorial(N)./(factorial(i).*factorial(N-i)))/(2.^N); Thanks.

回答 (1 件)

Roger Stafford
Roger Stafford 2014 年 2 月 17 日
編集済み: Roger Stafford 2014 年 2 月 17 日

0 投票

Believe it or not, the following code will compute your H, and in fact is probably more accurate than using factorials. It uses a technique known as the "Pascal Triangle". It differs only in the division by 2 at each step in computing P.
N = 32;
P = zeros(N+1,N+2);
P(1,2) = 1;
for n = 1:N
P(n+1,2:n+2) = (P(n,1:n+1)+P(n,2:n+2))/2;
end
h = zeros(N+1,1);
for n = 0:N
h(n+1) = -sum(P(n+1,2:n+2).*log2(P(n+1,2:n+2)));
end
The array 'h' here is such that H(n) = h(n+1). It gets all H from H(0) to H(32). If you want it at 0:2:32 do:
h(1:2:33)

1 件のコメント

Edy
Edy 2014 年 2 月 17 日
What it would be like if I use factorial formula instead of Pascal Triangle?

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

カテゴリ

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

タグ

質問済み:

Edy
2014 年 2 月 16 日

コメント済み:

Edy
2014 年 2 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by