Mercurial > forge
changeset 10380:10e03aab99f0 octave-forge
geometry: conversion form ellipse to covariance matrices.
author | jpicarbajal |
---|---|
date | Tue, 05 Jun 2012 12:23:02 +0000 |
parents | 53fafc737654 |
children | 09be49889ce3 |
files | main/geometry/inst/geom2d/cov2ellipse.m main/geometry/inst/geom2d/ellipse2cov.m |
diffstat | 2 files changed, 147 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/geometry/inst/geom2d/cov2ellipse.m Tue Jun 05 12:23:02 2012 +0000 @@ -0,0 +1,55 @@ +## Copyright (c) 2012 Juan Pablo Carbajal <carbajal@ifi.uzh.ch> +## +## This program is free software; you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program; if not, see <http://www.gnu.org/licenses/>. + +%% -*- texinfo -*- +%% @deftypefn {Function File} {@var{ellipse} = } cov2ellipse (@var{K}) +%% @deftypefnx {Function File} {[@var{ra} @var{rb} @var{theta}] = } cov2ellipse (@var{K}) +%% @deftypefnx {Function File} {@dots{} = } cov2ellipse (@dots{}, @samp{tol},@var{tol}) +%% Calculates ellipse parameters from covariance matrix. +%% +%% Run @code{demo cov2ellipse} to see an example. +%% +%% @seealso{ellipses2d, cov2ellipse, drawEllipse} +%% @end deftypefn + +function varargout = cov2ellipse (K, varargin); + + [R S W] = svd (K); + theta = atan (R(1,1)/R(2,2)); + v = sort (diag(S), 'ascend')'; + + if nargout == 1 + varargout{1} = [0 0 v theta*180/pi]; + elseif nargout == 3 + varargout{1} = v(1); + varargout{2} = v(2); + varargout{3} = theta; + end + +endfunction + +%!demo +%! K = [2 1; 1 2]; +%! L = chol(K,'lower'); +%! u = randn(1e3,2)*L'; +%! +%! elli = cov2ellipse (K) +%! +%! figure(1) +%! plot(u(:,1),u(:,2),'.r'); +%! hold on; +%! drawEllipse(elli,'linewidth',2); +%! hold off +%! axis tight
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/geometry/inst/geom2d/ellipse2cov.m Tue Jun 05 12:23:02 2012 +0000 @@ -0,0 +1,92 @@ +## Copyright (c) 2012 Juan Pablo Carbajal <carbajal@ifi.uzh.ch> +## +## This program is free software; you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program; if not, see <http://www.gnu.org/licenses/>. + +%% -*- texinfo -*- +%% @deftypefn {Function File} {@var{K} = } ellipse2cov (@var{elli}) +%% @deftypefnx {Function File} {@var{K} = } ellipse2cov (@var{ra}, @var{rb}) +%% @deftypefnx {Function File} {@var{K} = } ellipse2cov (@dots{}, @var{theta}) +%% Calculates covariance matrix from ellipse. +%% +%% If only one input is given, @var{elli} must define an ellipse as described in +%% @coomand{ellipses2d}. +%% If two inputs are given, @var{ra} and @var{rb} define the half-lenght of the +%% axes. +%% If a third input is given, @var{theta} must be the angle of rotation of the +%% ellipse in radians, and in counter-clockwise direction. +%% +%% The output @var{K} contains the covariance matrix define by the ellipse. +%% +%% Run @code{demo ellipse2cov} to see an example. +%% +%% @seealso{ellipses2d, cov2ellipse, drawEllipse} +%% @end deftypefn + +function K = ellipse2cov (elli, varargin); + + ra = 1; + rb = 1; + theta = 0; + switch numel (varargin) + case 0 + %% ellipse format + if numel (elli) != 5 + print_usage (); + end + ra = elli(1,3); + rb = elli(1,4); + theta = elli(1,5)*pi/180; + + case 2 + %% ra,rb + if numel (elli) != 1 + print_usage (); + end + ra = elli; + rb = varargin{1}; + + case 3 + %% ra,rb, theta + if numel (elli) != 1 + print_usage (); + end + ra = elli; + rb = varargin{1}; + theta = varargin{2}; + + otherwise + print_usage (); + end + + T = createRotation (theta)(1:2,1:2); + K = T*diag([ra rb])*T'; + +endfunction + +%!demo +%! elli = [0 0 1 3 -45]; +%! +%! % Create 2D normal random variables with covarinace defined by elli. +%! K = ellipse2cov (elli) +%! L = chol(K,'lower'); +%! u = randn(1e3,2)*L'; +%! +%! Kn = cov (u) +%! +%! figure(1) +%! plot(u(:,1),u(:,2),'.r'); +%! hold on; +%! drawEllipse(elli,'linewidth',2); +%! hold off +%! axis tight