2012-02-16 163 views
0

我有導入文本文件到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行)。

任何意見將不勝感激。

回答

1

我想這你想要做什麼。

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,則這會給您帶來錯誤。