Code: function c = bsp0201(P) n = length(P); G = [P; [1:n]]; G = sortBy1stRow(G, 'descend'), for i = 1 : n-1 G(1,n-i) = G(1,n-i) + G(1,n-i+1); L(i) = G(2,n-i); R(i) = G(2,n-i+1); G(2,n-i) = n+i; G = sortBy1stRow(G(:, [1:n-i]), 'descend'), end%for c = cell(1, G(2,1)-1); c{L(n-1)} = '0'; c{R(n-1)} = '1'; for j = n-2 : -1 : 1 c{L(j)} = strcat(c{j+n}, '0'); c{R(j)} = strcat(c{j+n}, '1'); end%for end%function
function M = sortBy1stRow(M, mode) [r, s] = sort(M(1,:), mode); M = M(:, s); end%function |