Daniel@0: function Y = sample_cond_multinomial(X, M) Daniel@0: % SAMPLE_MULTINOMIAL Sample Y(i) ~ M(X(i), :) Daniel@0: % function Y = sample_multinomial(X, M) Daniel@0: % Daniel@0: % X(i) = i'th sample Daniel@0: % M(i,j) = P(Y=j | X=i) = noisy channel model Daniel@0: % Daniel@0: % e.g., if X is a binary image, Daniel@0: % Y = sample_multinomial(softeye(2, 0.9), X) Daniel@0: % will create a noisy version of X, where bits are flipped with probability 0.1 Daniel@0: Daniel@0: if any(X(:)==0) Daniel@0: error('data must only contain positive integers') Daniel@0: end Daniel@0: Daniel@0: Y = zeros(size(X)); Daniel@0: for i=min(X(:)):max(X(:)) Daniel@0: ndx = find(X==i); Daniel@0: Y(ndx) = sample_discrete(M(i,:), length(ndx), 1); Daniel@0: end Daniel@0: Daniel@0: