MATLAB: How can i compute probabalies in haffmaneco

MATLAB

how i can i compute probabalties to compress all letters small capital and numbers

Best Answer

[unique_symbols, ~, bin_number] = unique(YourText(:));
symbol_counts = accumarray(bin_number, 1);
symbol_probs = symbol_counts ./ numel(YourText);
Alternately,
sparse_count = sparse(1, double(YourText(:)), 1);
[~, unique_symbols, symbol_counts] = find(sparse_count);
unique_symbols = char(unique_symbols.');
symbol_probs = symbol_counts.' ./ numel(YourText);
In both cases, after the code, unique_symbols will be a column vector of characters that exist in the text, and symbol_probs will be the corresponding probability.
The sparse() version has a minor flaw that it will fail if any characters in the text are binary 0.