2013-03-12 72 views
0

我目前正在Matlab中進行一個項目,其中我有一個單元陣列的單元陣列。第一個單元陣列長464列,深1行。這些單元中的每一個都是另一個96列365行的單元陣列。我需要能夠得到每個464數組的96列的平均值,並將464個數組中的每一個放置在一個名爲平均數的新數組的不同行中。我曾嘗試編寫代碼來只做一列如下:如何從數組中的數組中取列的均值?

mean = Homes{1,1}(1:) 

但我當過我嘗試運行這段代碼,我得到了如下錯誤:

mean = Homes{1,1}(1:) 
         | 
    Error: Unbalanced or unexpected parenthesis or bracket. 

基本上我的最後一個數組名是什麼意思需要96列464行。我被卡住了,真的可以使用你的幫助。

謝謝。

+0

'(1:)'肯定是行不通的,你可以嘗試'(1,:)'。我永遠不知道如何處理單元陣列。我想如果你通過頂層單元格陣列進行循環,並將每個單元格分配給一個臨時變量,那麼你肯定可以得到一個意思。 – 2013-03-12 20:49:44

+0

嘗試使用cellfun。您可以嘗試cellfun(@ mean,Homes),這將對單元格數組中的每個元素應用均值函數。 – Justin 2013-03-12 21:58:14

+0

當我嘗試cellfun(@ mean,Homes)時,我收到了此消息... cellfun(@ mean,Homes) 未定義的函數'sum'用於輸入 類型爲'cell'的參數。 平均誤差(第28行) y = sum(x)/ size(x,dim); – user2093732 2013-03-12 22:13:53

回答

0

我建議你在較小的矩陣上嘗試下面的代碼。看看它是否給你想要的結果。

a=cell(1,4); %for you it will be a=cell(1,464) 
for i=1:4 
    a{i}=randi(10,[5 10]); %for you it will be a{i}=randi(10,[365 96]); 
end 
a1=cell2mat(a); %just concatenating 
a1=mean(a1); %getting the mean for each column. in your case, you should get the mean for 96*464 
a2=reshape(a1,[10 4]); %now what reshape does it it takes first 10 elements and arranges it into first column. 
%Therefore, since I finally want a 4x10 matrix (in your case 464x96), i.e. mean of 10 elements in first cell array on first row and so on... 
%Thus, 1st 10 elements should go to first column after doing reshape (since you want to keep them together). Therefore, instead of directly making matrix as 4x10, first make it as 10x4 and then take transpose (which is the next step). 

a2=a2'; %thus you get a 4x10 matrix. 

在你的情況而言,代碼會

a=cell(1,464); 
for i=1:464 
    a{i}=randi(10,[365 96]); 
end 
a1=cell2mat(a); 
a1=mean(a1); 
a2=reshape(a1,[96 365]);       
a2=a2';