Mean of Cells of Cells
2 ビュー (過去 30 日間)
古いコメントを表示
So I have cell arrays that contain other cells The structure looks like this:
Cell{X} always contains 3 sub-cells, the first sub-cell always contains a 16x107 matrix, the second one a 16x47 matrix and the third one a 16x147 matrix. These are matrices with those dimensions. I want to take the (nan)mean of all same-sized matrices, in some way that is the intuitive equivalent of this: nanmean(Cell{1:end}{1}), nanmean(Cell{1:end}{2}), nanmean(Cell{1:end}{3}), etc. The mean should be taken along the 1st dimension so I end up with with 3 mean matrices, one 16x107, one 16x47, and one 16x147.
Obviously the above ways don't work ('Bad cell reference operation'). How can I do this without looping through everything and adding them up one by one?
To Clarify:
My data:
Cell_1(cell_1(Matrix),cell_2(Matrix),cell_3(Matrix))
Cell_2(cell_1(Matrix),cell_2(Matrix),cell_3(Matrix))
Cell_3(cell_1(Matrix),cell_2(Matrix),cell_3(Matrix))
Cell_4(cell_1(Matrix),cell_2(Matrix),cell_3(Matrix))
I want to get 3 mean matrices, one of all cell_1(Matrix), one of all cell_2(Matrix), and one of all cell_3(Matrix), where the 3 mean matrices are of size cell_1(Matrix), cell_2(Matrix), and cell_3(Matrix). I do not want to take means along the dimensions within a given cell(Matrix), but along the dimensions of Cell, NOT cell.
0 件のコメント
採用された回答
Azzi Abdelmalek
2013 年 2 月 15 日
編集済み: Azzi Abdelmalek
2013 年 2 月 15 日
Try this
Edit
%-----------Your array--------------------------------------------------
Y1={num2cell(rand(16,107)),num2cell(rand(16,47)),num2cell(rand(16,147))};
Y2={num2cell(rand(16,107)),num2cell(rand(16,47)),num2cell(rand(16,147))};
Y3={num2cell(rand(16,107)),num2cell(rand(16,47)),num2cell(rand(16,147))};
Y4={num2cell(rand(16,107)),num2cell(rand(16,47)),num2cell(rand(16,147))};
Ycell={Y1,Y2,Y3,Y4}
%----------------Your code------------------------------------------------
n=numel(Ycell);
m=numel(Ycell{1});
M=cell(n,m);
for p=1:m
for k=1:n
M{k,p}=cell2mat(Ycell{k}{p});
end
[ii,jj]=size(M{k,p});
a=cell2mat(M(1:n,p));
b=reshape(a',jj,ii,[]);
out{p}=nanmean(b,3)';
end
out
3 件のコメント
その他の回答 (2 件)
Azzi Abdelmalek
2013 年 2 月 14 日
編集済み: Azzi Abdelmalek
2013 年 2 月 14 日
Yourcell={rand(16,107),rand(16,47),rand(16,147)}
out=cellfun(@(x) nanmean(x(:)),Yourcell,'un',0)
%or
Yourcell={rand(16,107),rand(16,47),rand(16,147)}
out=cellfun(@nanmean,Yourcell,'un',0)
7 件のコメント
Azzi Abdelmalek
2013 年 2 月 14 日
編集済み: Azzi Abdelmalek
2013 年 2 月 14 日
Ok
out=cellfun(@(y) cellfun(@(x) nanmean(cell2mat(x(:))),y,'un',0),Ycell,'un',0)
res=nanmean(cell2mat(cellfun(@(x) cell2mat(x)',out,'un',0)),2)
Sean de Wolski
2013 年 2 月 14 日
編集済み: Sean de Wolski
2013 年 2 月 14 日
I think you need two cellfuns with the inner-cellfun being part of the anonymous function for the first:
cellfun(@(c)cellfun(@(x)nanmean(x),c,'uni',false),your_cell,'uni',false)
参考
カテゴリ
Help Center および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!