【问题标题】:matlab averages of cell array单元阵列的matlab平均值
【发布时间】:2012-02-16 10:02:19
【问题描述】:

我有以下脚本用于将文本文件导入 matlab,其中包括每小时数据,然后我尝试将它们转换为每日平均值:

  clear all 
  pathName = ...
  TopFolder = pathName; 
  dirListing = dir(fullfile(TopFolder,'*.txt'));%Lists the folders in the directory specified by pathName.

  for i = 1:length(dirListing);
      SubFolder{i} = dirListing(i,1).name;%obtain the name of each folder in
              %the specified path.
  end

  %import data
    for i=1:length(SubFolder);
        rawData1{i} = importdata(fullfile(pathName,SubFolder{i}));
    end


  %convert into daily averages
    rawData2=cell2mat(rawData1);
        %create one matrix for entire data set
    altered=reshape(rawData2,24,(size(rawData2,2)*365));
        %convert into daily values
    altered=mean(altered)';
        %take the average for each day
    altered=reshape(altered,365,size(rawData2,2));
        %convert back into original format

我的问题在于尝试将数据转换回与“rawData1”相同的格式,这是每个变量的一个单元格(每个变量都由“SubFolder”表示。这样做的原因是除了一个变量为向量,其余变量为矩阵(8760*11)。

所以,这方面的一个例子是:

  clear all 

  cell_1 = rand(8760,1); 
  cell_2 = rand(8760,1);
  cell_3 = rand(8760,1);
  cell_4 = rand(8760,1);
  cell_5 = rand(8760,1);
  cell_6 = rand(8760,11);
  cell_7 = rand(8760,1);
  cell_8 = rand(8760,1);
  cell_9 = rand(8760,1);

  data = {cell_1,cell_2,cell_3,cell_4,cell_5,cell_6,cell_7,cell_8,cell_9};

我需要将“数据”中的每个单元格从每小时值转换为每日平均值(即 365 行)。

任何建议将不胜感激。

【问题讨论】:

    标签: matlab cell average


    【解决方案1】:

    我认为这可以满足您的需求。

    data = cellfun(@(x) reshape(mean(reshape(x,24,[]))',365,[]),data,'uniformoutput',false);
    

    但是这有点令人困惑,所以我会稍微解释一下。

    cellfun 内部的这部分 mean(reshape(x,24,[]))' 会将数据中的每个单元格重新整形为 24 x 365,计算平均值,然后将其转换回单列。当原始数据只有 1 列时,这很好用……但是对于具有 11 列的 cell_6,它将所有数据首尾相连。所以我在mean(...) 部分周围添加了一个附加的reshape(...) 包装器,以将其放回原来的11 列......或更精确的N 列,长度为365 行。

    注意:如果您的数据集维度不是 X 的 8760,这将给您带来错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-02
      • 2015-12-03
      • 2011-10-21
      • 2017-10-30
      • 1970-01-01
      • 2013-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多