MATLAB:将标题添加到矩阵

时间:2013-11-15 10:04:58

标签: matlab

数据

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

包含在三个不同商店销售产品5​​天。因此该列表示商店1,2& 3行表示周一至周五。

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

 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

如何添加标题?

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:3)

对于早于2013b的Matlab版本,您可以使用单元格阵列。

答案 2 :(得分:1)

如果您拥有统计工具箱,则“dataset”可以包含列的标题和说明。 (参见例如this answer