spm_multrnd.m 568 B

12345678910111213141516171819
  1. function [m] = spm_multrnd(p,N)
  2. % Sample from multinomial distribution
  3. % FORMAT [m] = spm_multrnd(p,N)
  4. %
  5. % p - [M x 1] vector of probabilities
  6. % N - Number of samples to generate
  7. %
  8. % m - [N x 1] vector of samples, where each sample is number from 1 to M
  9. %__________________________________________________________________________
  10. % Copyright (C) 2009 Wellcome Trust Centre for Neuroimaging
  11. % Will Penny
  12. % $Id: spm_multrnd.m 3190 2009-06-08 17:13:36Z guillaume $
  13. cp = [0; cumsum(p(:))];
  14. m = zeros(N,1);
  15. for n=1:N
  16. m(n) = find(rand > cp, 1, 'last');
  17. end