function [code1, average_length] = ShannonFanoFunc(p) set(0,'RecursionLimit',1e4); % p1 - probability vector % code1 - corresponds codewords %average_length is the expected codeword length % check if p1 is row vector or column vector if ((sum(p>=0)~=length(p))) error('Enter a probability vector'); end p = p/sum(p); if(length(p)>2) [pdes,idx] = sort(p,'descend'); % sort in descending order and maintain previous index .. to access simply p1(idx); qsum = (2*cumsum(pdes))-1; %cumulative sum of A starting at the beginning of the first array dimension in A whose size does not equal 1. [~,idx1] = min(abs(qsum)); % find min. value and discard it ... keep its index as we need index only if((idx1>1)&&(idx1