Matlab 使用cellfun作为cell的平均值

Matlab 使用cellfun作为cell的平均值,matlab,Matlab,我有一个手机: BED4{6,4,4}=[]; eg. BED4{1}{1}{1} ans = [8x1 double] [8x1 double] [8x2 double] [8x1 double] 我想计算平均值,就像是通过沿红色箭头的for循环: 到目前为止我不得不这么做 figure('color',[1 1 1]) titles={'Direct care','Housekeeping','Mealtimes','Medication rounds',

我有一个手机:

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

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

    [8x1 double]    [8x1 double]    [8x2 double]    [8x1 double]
我想计算
平均值
,就像是通过沿红色箭头的for循环:

到目前为止我不得不这么做

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
其中:


如果您重新调整数据形状以隔离第一个维度(
care
):

每行对应一个不同的值
care
。让我们将每行中的单元格内容堆叠为三维矩阵,并获得所需的平均值:

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
应包含所有所需的平均值。

对于手绘图形始终为a+1:)@RodyOldenhuis+1对于画笔+1用于用黑色细笔绘制红色粗箭头。(这已经算魔术了吗?)+1用于在纸上使用MATLAB语法:)数据的结构太嵌套了。此外,您提供的信息不足以设计向量化的内容,例如,BED4{n}{n}{n}中三个单元格的大小是否总是相同?非常好!非常感谢你!手绘可以帮助我看到数据的样子,只要我能用手(字面上)对其执行操作就好了。
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