2013-05-16 101 views
7

的平均我有一個單元:使用cellfun細胞

BED4{6,4,4}=[]; 

eg. BED4{1}{1}{1} 
ans = 

    [8x1 double] [8x1 double] [8x2 double] [8x1 double] 

我想計算mean就好像它是通過對沿紅色箭頭循環:

enter image description here

到目前爲止,我必須這樣做...

figure('color',[1 1 1]) 
titles={'Direct care','Housekeeping','Mealtimes','Medication rounds','Miscellaneous care','Personal care'}; 

for care=1:6 
    subplot(3,2,care) 
    clear a m e pci1 pci2 gn 
for position=1:4 
    for patient=1:4 
a(:,:,position,patient,care)=cell2mat(BED4{care}{position}{patient}); 
    end 
end 

m=mean(mean(a(:,1,:,:,care),4),3); 
e=mean(mean(a(:,2,:,:,care),4),3); 
pci1=mean(mean(a(:,3,:,:,care),4),3); 
pci2=mean(mean(a(:,4,:,:,care),4),3); 
gn=a(:,5,1,1,care); 
if care==1 
    b={m,e,pci1,pci2,gn}; %store for posterity 
end 

h=errorbar(gn,m,e,'sr'); 

set(h,'linestyle','--','LineWidth',2,... 
       'MarkerEdgeColor','k',... 
       'MarkerFaceColor','white',... 
       'MarkerSize',5); 
ylabel('Relative Risk '); 
xlabel('Patient contact count'); 
title(titles{ii}) 
set(gca,'xtick',0:2:8) 
axis([-2 8 0 1]) 
end 

其中給出:

enter image description here

+8

總是+1手繪數字:) –

+3

@RodyOldenhuis +1爲筆! – Shai

+3

+1用於用黑色薄筆繪製厚厚的紅色箭頭。 (這是否已經算作魔術了?) –

回答

1

如果你重塑你的數據隔離的第一維(care)它更容易:

C = reshape(BED4, size(BED4, 1), 1); 

每一行對應的care不同的值。讓我們堆疊的單元的含量在每行中作爲3 d矩陣和獲得所期望的平均值:

res = cell(size(C, 1), 1);    %// Preallocate memory 
for care = 1:size(C, 1) 
    X = vertcat(C{care, :});    %// Obtain content from row 
    func = @(k)mean(cat(3, X{:, k}), 3); %// Stacks content and obtains mean 
    res{care} = arrayfun(func, 1:size(X, 2), 'UniformOutput', 0); 
end 

將得到的細胞陣列res應該包含所有所需的平均值。

+1

非常好!非常感謝你!手繪圖幫我看看數據是什麼樣子的,只要我能手工操作就可以了(數字)。 – HCAI