我建議你在較小的矩陣上嘗試下面的代碼。看看它是否給你想要的結果。
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';
'(1:)'肯定是行不通的,你可以嘗試'(1,:)'。我永遠不知道如何處理單元陣列。我想如果你通過頂層單元格陣列進行循環,並將每個單元格分配給一個臨時變量,那麼你肯定可以得到一個意思。 – 2013-03-12 20:49:44
嘗試使用cellfun。您可以嘗試cellfun(@ mean,Homes),這將對單元格數組中的每個元素應用均值函數。 – Justin 2013-03-12 21:58:14
當我嘗試cellfun(@ mean,Homes)時,我收到了此消息... cellfun(@ mean,Homes) 未定義的函數'sum'用於輸入 類型爲'cell'的參數。 平均誤差(第28行) y = sum(x)/ size(x,dim); – user2093732 2013-03-12 22:13:53