function showTransitionProbabilities(sequence) if isa(sequence,'Signal') sequence = sequence.signal; end sequence = sequence(2+mod(1,length(sequence)):end); %filtered sequences often have one "old" sample at idx=1 x = sequence; levels = sort(unique(x)).'; % or provide known 1x6 level values [~,ix] = min(abs(x - levels),[],2); x = levels(ix); K = numel(levels); % map to state indices 1..K [tf, idx] = ismember(x, levels); idx = idx(:); % from = idx(1:end-1); % to = idx(2:end); from = idx(1:2:end); to = idx(2:2:end); % counts C(from,to) C = accumarray([from,to], 1, [K K], @sum, 0); % row-stochastic transition matrix P(to|from) rowSums = sum(C,2); P = C ./ max(rowSums,1); %% 1) HEATMAP (which transitions are more probable?) figure('Name','Transition Probabilities (to | from)'); h = heatmap(levels, levels, P, 'Colormap', parula, 'ColorbarVisible','on'); colormap(gca,[[1,1,1];flip(cbrewer2('Spectral',100))]);clim([0,ceil(max(P(:))*10)/10]); h.XLabel = 'From state (level)'; h.YLabel = 'To state (level)'; h.Title = 'P(to | from)'; end