1

数据

  x=[33 44 29; 16 25 45; 33 19 54; 22 21 49; 11 24 56];

包含在三个不同的商店中销售 5 天的产品。所以列表示 shop 1,2 & 3 并且行表示 Monday to Friday 。

现在我计算了每天的汇总统计数据。

 meanday=mean(x');
 medianday=median(x');
 minday=min(x');
 maxday=max(x');
 varianceday=var(x');
 summaryday=[meanday;medianday;minday;maxday;varianceday];

现在我想添加这样的标题:

 summaryday =

                Mean       Median   Minimum   Maximum    variance
  Monday        32.3333   28.6667   35.3333   27.6667   30.3333
  Tuesday       33.0000   25.0000   33.0000   22.0000   24.0000
  Wednesday     20.0000   16.0000   19.0000   21.0000   11.0000
  Thursday      44.0000   45.0000   54.0000   40.0000   56.0000
   Friday       144.3333  220.3333  310.3333  114.3333  536.3333

如何添加标题?

4

3 回答 3

7

如果您没有 R2013b 或更高版本,最好的解决方案是围绕普通矩阵编写一个小类,它只实现构造函数和disp函数。

这是一个示例:

>> W = WeeklyStatistics(rand(7,5), 'Mean', 'Median', 'Minimum', 'Maximum', 'Variance')

W = 
                            Mean          Median         Minimum         Maximum        Variance
          Monday        0.228977        0.442678        0.084436        0.263803        0.853031
         Tuesday        0.913337        0.106653        0.399783        0.145539        0.622055
       Wednesday        0.152378        0.961898        0.259870        0.136069        0.350952
        Thursday        0.825817        0.004634        0.800068        0.869292        0.513250
          Friday        0.538342        0.774910        0.431414        0.579705        0.401808
        Saturday        0.996135        0.817303        0.910648        0.549860        0.075967
          Sunday        0.078176        0.868695        0.181847        0.144955        0.239916

>> 

这是我为此使用的课程:

classdef WeeklyStatistics < double

    properties
        columnTitles

        weekDays = {
            'Monday'
            'Tuesday'
            'Wednesday'
            'Thursday'
            'Friday'
            'Saturday'
            'Sunday'
        };

    end

    methods
        function obj = WeeklyStatistics(M, varargin)
            obj = obj@double(M);

            %// Just copy the data
            obj.columnTitles = varargin;

            %// NOTE: very basic checks (you might want to think a bit more
            %// about these
            if ~all(cellfun('isclass', obj.columnTitles, 'char'))
                error('WeeklyStatistics:invalid_column_titles',...
                    'Invalid column title format. All column titles should be given as strings.');
            end

            if size(obj,2) ~= numel(obj.columnTitles)
                error('WeeklyStatistics:dimension_mismatch',...
                    'Number of columns in data disagrees with number of column titles.');
            end

            if size(obj,1) > numel(obj.weekDays)
                error('WeeklyStatistics:dimension_mismatch',...
                    'Number of rows in data exceeds number of days in a week.');
            end

        end

        %// These are needed to make the matrix behave like an ordinary matrix       
        function R = subsref(obj,varargin)
            R = subsref(double(obj), varargin{:}); end        
        function R = subsasgn(obj,varargin)
            R = WeeklyStatistics(subsasgn(double(obj), varargin{:}), obj.columnTitles{:}); end

        %// Show the matrix      
        function disp(obj)

            %// Print column titles
            fprintf(1,repmat(' ',1,16));
            fprintf(1,'%16s', obj.columnTitles{:});
            fprintf(1,'\n');

            %// Print data for all weekdays            
            dobj = double(obj);            
            for ii = 1:size(dobj,1)
                fprintf(1, '%16s', obj.weekDays{ii});
                fprintf(1,'%16f',dobj(ii,:));
                fprintf(1,'\n');
            end
            fprintf(1,'\n');

            %// NOTE: proper disp() implementations check the current setting
            %// of format() and adjust spacing and formatting accordingly

        end        
    end    
end
于 2013-11-15T10:31:51.420 回答
3

对于 2013b 之前的 Matlab 版本,您可以使用元胞数组。

于 2013-11-15T10:19:56.290 回答
1

如果您有统计工具箱,则“数据集”可以包含列的标题和描述。(参见例如这个答案

于 2013-11-15T10:38:43.093 回答