1## Copyright (C) 2007 Lukasz Komsta, http://www.komsta.net/
2##
3## This program is free software; you can redistribute it and/or modify
4## it under the terms of the GNU General Public License as published by
5## the Free Software Foundation; either version 2 of the License, or
6## (at your option) any later version.
7##
8## This program is distributed in the hope that it will be useful,
9## but WITHOUT ANY WARRANTY; without even the implied warranty of
10## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11## GNU General Public License for more details.
12##
13## You should have received a copy of the GNU General Public License
14## along with this program; If not, see <http://www.gnu.org/licenses/>.
15
16## Find value with largest difference from the mean
17##
18## Description:
19##
20##      Finds value with largest difference between it and sample mean,
21##      which can be an outlier.
22##
23## Usage:
24##
25##      [out] = outlier(x,opposite,logical)
26##
27## Arguments:
28##
29##        x: a data sample, vector in most cases. If argument is a
30##          matrix, each column is treated as independent dataset.
31##
32## opposite: if set to 1 (default 0), gives opposite value (if largest value has
33##           maximum difference from the mean, it gives smallest and vice
34##           versa)
35##
36##  logical: if set to 1 (default 0), gives vector of logical values, and possible
37##           outlier position is marked by 1, others are 0
38##
39## Value:
40##
41##      A vector of value(s) with largest difference from the mean.
42##
43## Author(s):
44##
45##      Lukasz Komsta, ported from R package "outliers".
46##	See R News, 6(2):10-13, May 2006
47##
48##
49
50
51function [out] = outlier(x,opposite,logical)
52
53	if nargin<3
54		logical=0;
55	end
56	if nargin<2
57		opposite=0;
58	end
59
60   if ~isvector(x) & ismatrix(x)
61	out = [];
62	for i=1:columns(x)
63		oo = outlier(x(:,i),opposite,logical);
64		out = [out oo];
65	end
66   elseif isvector(x)
67
68	if (xor(((max(x) - mean(x)) < (mean(x) - min(x))), opposite))
69            if ~logical
70                out = min(x);
71            else
72		out = (x == min(x));
73	    end
74        else
75            if ~logical
76                out = max(x);
77            else
78		out = (x == max(x));
79	    end
80	end
81
82    else
83	    error("x must be a vector or a matrix");
84    end
85
86end
87
88