MATLAB平均
问题描述:
我有导入文本文件到MATLAB下面的脚本,其中包括每小时的数据,在这里我再尝试将它们转换成每日平均值: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'相同的格式,'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行)。
任何意见将不胜感激。
答
我想这你想要做什么。
data = cellfun(@(x) reshape(mean(reshape(x,24,[]))',365,[]),data,'uniformoutput',false);
当然,这是一种混淆,所以我会解释一下。
这部分mean(reshape(x,24,[]))'
里面的cellfun
将数据中的每个单元格重新整形为24×365,计算平均值,然后将其重新转换为单个列。这在原始数据只有1列时工作正常......但对于具有11列的cell_6,它将所有数据首尾相连。所以我添加围绕mean(...)
一部分除了reshape(...)
包装把它放回原来的11列...以上precises N列,其长度365行。
注意:如果您有数据集尺寸不是X的8760,则这会给您带来错误。