Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

drawgaussian.m 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. %drawgaussian Draw the 1-sigma lines for a 2d gaussian
  2. %
  3. % [h] = drawgaussian(mean, [sigmax, sigmay, r | C], ...
  4. % {'angstart', 0}, {'angend', 360}, {'useC', 0})
  5. %
  6. % Draws the nth-sigma lines for a gaussian in the current
  7. % figure. Since it uses LINE to do this, it returns a handle to
  8. % that line.
  9. %
  10. % 'mean' must be a 2 element vector, first component <x>, second
  11. % <y>.
  12. %
  13. % sigmax is the standard deviation of x; sigma y the standard
  14. % deviation of y; and r is defined as
  15. % <(x-<x>)*(y-<y>)>/(sigmax*sigmay) and thus lies in [0,1). If
  16. % only two arguments are passed, the second is assumed to be a
  17. % two-by-two covariance matrix.
  18. %
  19. function [hout] = drawgaussian(mean, sigmax, sigmay, r, varargin)
  20. pairs = { ...
  21. 'angstart' 0 ; ...
  22. 'angend' 360 ; ...
  23. 'useC' 0 ; ...
  24. 'filled' 0 ; ...
  25. 'alph' 1 ; ...
  26. 'nth_sigma' 1 ; ...
  27. 'drawline' 0 ; ...
  28. }; parseargs(varargin, pairs);
  29. if ~isempty(find(isnan(sigmax(:)))) || ...
  30. (nargin>2 && ~isempty(find(isnan(sigmay(:))))),
  31. hout = [];
  32. return;
  33. end;
  34. npoints = 100;
  35. X = zeros(2, npoints+1);
  36. p = zeros(2,1);
  37. if nargin <= 2 || useC,
  38. covar = sigmax;
  39. else
  40. covar = [sigmax^2, r*sigmax*sigmay; ...
  41. r*sigmax*sigmay, sigmay^2];
  42. end;
  43. [E, D] = eig(covar);
  44. D = nth_sigma * sqrt(D); % sigma, not sigma^2
  45. for i=1:npoints
  46. theta = (angend - angstart)*(i-1)/(npoints-1) + angstart;
  47. theta = pi*theta/180;
  48. p(1) = D(1,1) * cos(theta);
  49. p(2) = D(2,2) * sin(theta);
  50. X(:,i) = E*p;
  51. end;
  52. if rem(angend,360) == rem(angstart,360),
  53. X(:,end) = X(:,1);
  54. else
  55. X = X(:,1:end-1);
  56. end;
  57. if drawline, edgecolor = 'k'; else edgecolor = 'none'; end;
  58. if filled,
  59. h = patch(X(1,:)+mean(1), X(2,:)+mean(2), 'r', 'FaceAlpha', min(1, alph), 'EdgeColor', edgecolor);
  60. else
  61. h = line(X(1,:)+mean(1), X(2,:)+mean(2));
  62. end;
  63. if nargout > 0,
  64. hout = h;
  65. end;