Skip to content

Commit

Permalink
Table: sum and median can now operate also on dimension 2
Browse files Browse the repository at this point in the history
  • Loading branch information
dlegland committed Jan 7, 2021
1 parent b02be7b commit 136329a
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 16 deletions.
12 changes: 9 additions & 3 deletions matStats/@Table/mean.m
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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 = '';
Expand Down
33 changes: 27 additions & 6 deletions matStats/@Table/median.m
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -13,7 +16,7 @@
% median 5.8 3 4.35 1.3
%
% See also
% mean, std
% mean, std, sum
%

% ------
Expand All @@ -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);

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
35 changes: 28 additions & 7 deletions matStats/@Table/sum.m
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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.

Expand All @@ -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);

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
12 changes: 12 additions & 0 deletions tests/test_mean.m
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
12 changes: 12 additions & 0 deletions tests/test_median.m
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
12 changes: 12 additions & 0 deletions tests/test_sum.m
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

0 comments on commit 136329a

Please sign in to comment.