diff --git a/matStats/@Table/mean.m b/matStats/@Table/mean.m index b8b2b9a..9801393 100644 --- a/matStats/@Table/mean.m +++ b/matStats/@Table/mean.m @@ -18,12 +18,12 @@ % mean 5.8433 3.054 3.7587 1.1987 % % See also -% median, std, var +% median, std, var, sum % % ------ % Author: David Legland -% e-mail: david.legland@inra.fr +% e-mail: david.legland@inrae.fr % Created: 2011-06-17, using Matlab 7.9.0.529 (R2009b) % Copyright 2011 INRA - Cepia Software Platform. @@ -32,9 +32,15 @@ error('Can not compute mean for table with factors'); end +% parse dimension to process dim = 1; if ~isempty(varargin) - dim = varargin{1}; + var1 = varargin{1}; + if isnumeric(var1) && isscalar(var1) + dim = var1; + else + error('Can not interpret optional argument'); + end end newName = ''; diff --git a/matStats/@Table/median.m b/matStats/@Table/median.m index 8112370..3ff9091 100644 --- a/matStats/@Table/median.m +++ b/matStats/@Table/median.m @@ -5,6 +5,9 @@ % Computes the median of each column in the table, and put the result in % a new table. % +% RES = median(TAB, DIM) +% Specifies the dimension for computing sum. Default is 1. +% % Example % iris = Table.read('fisherIris.txt'); % median(iris(:,1:4)) @@ -13,7 +16,7 @@ % median 5.8 3 4.35 1.3 % % See also -% mean, std +% mean, std, sum % % ------ @@ -28,14 +31,32 @@ error('Can not compute median for table with factors'); end +% parse dimension to process +dim = 1; +if ~isempty(varargin) + var1 = varargin{1}; + if isnumeric(var1) && isscalar(var1) + dim = var1; + else + error('Can not interpret optional argument'); + end +end newName = ''; if ~isempty(obj.Name) newName = ['Median of ' obj.Name]; end -res = Table.create(median(obj.Data, 1), ... - 'rowNames', {'median'}, ... - 'colNames', obj.ColNames, ... - 'name', newName); - \ No newline at end of file +if dim == 1 + res = Table.create(median(obj.Data, 1), ... + 'Parent', obj, ... + 'RowNames', {'median'}, ... + 'Name', newName); + +else + res = Table.create(median(obj.Data, 2), ... + 'Parent', obj, ... + 'ColNames', {'median'}, ... + 'Name', newName); + +end \ No newline at end of file diff --git a/matStats/@Table/sum.m b/matStats/@Table/sum.m index 7dd0add..59ec847 100644 --- a/matStats/@Table/sum.m +++ b/matStats/@Table/sum.m @@ -2,6 +2,10 @@ % Put the sum of each column in a new table. % % RES = sum(TAB) +% Computes the sum of each column in the table. +% +% RES = sum(TAB, DIM) +% Specifies the dimension for computing sum. Default is 1. % % Example % tab = Table.read('fisherIris.txt'); @@ -12,12 +16,12 @@ % % % See also -% mean, plus +% mean, sum % % ------ % Author: David Legland -% e-mail: david.legland@inra.fr +% e-mail: david.legland@inrae.fr % Created: 2011-06-17, using Matlab 7.9.0.529 (R2009b) % Copyright 2011 INRA - Cepia Software Platform. @@ -33,9 +37,26 @@ newName = ['Sum of ' obj.Name]; end +% parse direction +dim = 1; +if ~isempty(varargin) + var1 = varargin{1}; + if isnumeric(var1) && isscalar(var1) + dim = var1; + else + error('Can not interpret optional argument'); + end +end + % create result table -res = Table.create(sum(obj.Data, 1), ... - 'rowNames', {'sum'}, ... - 'colNames', obj.ColNames, ... - 'name', newName); - \ No newline at end of file +if dim == 1 + res = Table.create(sum(obj.Data, 1), ... + 'RowNames', {'sum'}, ... + 'ColNames', obj.ColNames, ... + 'Name', newName); +elseif dim == 2 + res = Table.create(sum(obj.Data, 2), ... + 'RowNames', obj.RowNames, ... + 'ColNames', {'sum'}, ... + 'Name', newName); +end \ No newline at end of file diff --git a/tests/test_mean.m b/tests/test_mean.m index e144860..af34200 100644 --- a/tests/test_mean.m +++ b/tests/test_mean.m @@ -27,3 +27,15 @@ function test_Simple(testCase) %#ok<*DEFNU> assertTrue(testCase, isa(res, 'Table')); assertEqual(testCase, res.Data, mean(data), 'AbsTol', .01); + + +function test_Dim2(testCase) %#ok<*DEFNU> +% Test call of function without argument. + +data = ones([6 4]); +tab = Table(data); + +res = mean(tab, 2); + +assertTrue(testCase, isa(res, 'Table')); +assertEqual(testCase, size(res), [6 1]); diff --git a/tests/test_median.m b/tests/test_median.m index a7a74df..ee5336b 100644 --- a/tests/test_median.m +++ b/tests/test_median.m @@ -27,3 +27,15 @@ function test_Simple(testCase) %#ok<*DEFNU> assertTrue(testCase, isa(res, 'Table')); assertEqual(testCase, res.Data, median(data), 'AbsTol', .01); + + +function test_Dim2(testCase) %#ok<*DEFNU> +% Test call of function without argument. + +data = ones([6 4]); +tab = Table(data); + +res = median(tab, 2); + +assertTrue(testCase, isa(res, 'Table')); +assertEqual(testCase, size(res), [6 1]); diff --git a/tests/test_sum.m b/tests/test_sum.m index 4c2d8a2..9aa4bee 100644 --- a/tests/test_sum.m +++ b/tests/test_sum.m @@ -27,3 +27,15 @@ function test_Simple(testCase) %#ok<*DEFNU> assertTrue(testCase, isa(res, 'Table')); assertEqual(testCase, res.Data, sum(data), 'AbsTol', .01); + + +function test_Dim2(testCase) %#ok<*DEFNU> +% Test call of function without argument. + +data = ones([6 4]); +tab = Table(data); + +res = sum(tab, 2); + +assertTrue(testCase, isa(res, 'Table')); +assertEqual(testCase, size(res), [6 1]);