我挺好奇为啥网上的香农编码实现都如此的冗长……明明四行就完事了:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
function shannon(varargin) probabilities = sort(cell2mat(varargin), 'descend') prefix_sum = [0, cumsum(probabilities(1 : end - 1))] code_length = -floor(log2(probabilities)) extractAfter(string(dec2bin(prefix_sum .* 2 .^ code_length))', max(code_length) - code_length) end
|
四行解决一切= =
本页代码适用Unlicense协议。