function S=snmd3(n,w,sigma,mu)

% SNMD3       Tri-variate normal mixture density random sample.
%
%             Example: load nmd3
%                      data=snmd3(100,wb,sigmab,mub); 
%                      kdedemo3
%
%             SNMD3(N,W,SIGMA,MU)
%
%             Christian C. Beardah 1998

% randn('seed',sum(100*clock));

S=zeros(n,3);

b(1)=w(1);
for i=2:length(w),
  b(i)=b(i-1)+w(i);
end;

for i=1:n,
  r=rand;
  for j=1:length(w),
    if r<b(j),
      S(i,1:3)=(sqrtm(sigma(1:3,(3*j-2):3*j))*randn(3,1)+mu(:,j))';
      break;
    end;
  end;
end;