假设我对你的理解正确,你可以按照以下方式做你想做的事(cmets 一步一步解释我做了什么):
% allocate space for the output
R = zeros(80, 90, 19);
% iterate over all 19 sets
for i=1:19
% extract ith set of 20 matrices to a separate cell
icell = {C{:,1,i}};
% concatenate all 20 matrices and reshape the result
% so that one matrix is kept in one column of A
% as a vector of size 80*90
A = reshape([icell{:}], 80*90, 20);
% sum all 20 matrices and calculate the mean
% the result is a vector of size 80*90
A = sum(A, 2)/20;
% reshape A into a matrix of size 80*90
% and save to the result matrix
R(:,:,i) = reshape(A, 80, 90);
end
您可以跳过提取到 icell 并直接连接第 i 组 20 个矩阵
A = reshape([C{:,1,i}], 80*90, 20);
我在这里只是为了清楚起见。
上面的步骤可以更简洁(但肯定更隐晦!)由以下arrayfun 调用表示:
F = @(i)(reshape(sum(reshape([C{:,1,i}], 80*90, 20), 2)/20, 80, 90));
R = arrayfun(F, 1:19, 'uniform', false);
R = reshape([R2{:}], 80, 90, 19);
匿名函数F 本质上是循环的一次迭代。 arrayfun 调用了 19 次,每组矩阵调用一次。我建议你坚持循环。