


sample from n-dim standard normal distribution Syntax: [pointMatrix, probabilityVector] = sampleNormal(nDim, nSample) OR .. = sampleNormal(.., mean, cov) THIS IS NO USER FUNCTION


0001 function [pointMatrix probabilityVector] = sampleNormal(... 0002 nDim, nSample, mean, cov) 0003 % sample from n-dim standard normal distribution 0004 % 0005 % Syntax: [pointMatrix, probabilityVector] = sampleNormal(nDim, nSample) 0006 % OR .. = sampleNormal(.., mean, cov) 0007 % 0008 % THIS IS NO USER FUNCTION 0009 0010 % The elk-library: convex geometry applied to crystallization modeling. 0011 % Copyright (C) 2013 Alexander Reinhold 0012 % 0013 % This program is free software: you can redistribute it and/or modify it 0014 % under the terms of the GNU General Public License as published by the 0015 % Free Software Foundation, either version 3 of the License, or (at your 0016 % option) any later version. 0017 % 0018 % This program is distributed in the hope that it will be useful, but 0019 % WITHOUT ANY WARRANTY; without even the implied warranty of 0020 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0021 % General Public License for more details. 0022 % 0023 % You should have received a copy of the GNU General Public License along 0024 % with this program. If not, see <http://www.gnu.org/licenses/>. 0025 0026 % The standard normal distribution has the parameters: 0027 % mean = zeros 0028 % cov = eye 0029 % A coordinate transformation of these x: 0030 % y = a + B*x 0031 % results in a normal distribution of y with parameters: 0032 % mean = a 0033 % cov = B*B' 0034 % Hence, given the parameters, the transformation parameters are: 0035 % a = mean 0036 % B = chol(cov) 0037 0038 % points 0039 pointMatrix = randn(nDim, nSample); 0040 0041 % some conveniance 0042 if ~exist('mean', 'var') || isempty(mean) 0043 mean = zeros(nDim, 1); 0044 end 0045 if ~exist('cov', 'var') || isempty(cov) 0046 cov = eye(nDim); 0047 end 0048 0049 % get the transformation matrix 0050 try 0051 B = chol(cov); 0052 catch me 0053 error('elk:distribution:wrongInput', ['The covariance matrix must ' ... 0054 'be symmetric and positive definite ''chol'' has thrown the ' ... 0055 'following message: ' me.message]); 0056 end 0057 0058 pointMatrix = bsxfun(@plus, B*pointMatrix, mean); 0059 probabilityVector = mvnpdf(pointMatrix', mean', cov)';