我有一个大小为 <20x1x19> 的元胞数组 C,每个元素都包含 19 个元素一组 20 个大小为 (80x90) 的矩阵,我如何计算每个 20 矩阵的平均值并将结果存储在矩阵 M 中,以便最终我将得到一个大小为 80x90x19 的矩阵,其中包含元胞数组矩阵的平均值。
例如:
M(:,:,1) 将具有 C(:,:,1) 中元素的平均值;
M(:,:,2) 将具有 C(:,:,2) 中元素的平均值
等等。
一点数组操作可以让您放弃循环。您可以更改元胞数组的维度,以便 cell2mat
生成 80×90×19×20 的数组,之后您需要做的就是沿维度取平均值 # 4:
%# C is a 20x1x19 cell array containing 80x90 numeric arrays
%# turn C into 1x1x19x20, swapping the first and fourth dimension
C = permute(C,[4 2 3 1]);
%# turn C into a numeric array of size 80-by-90-by-19-by-20
M = cell2mat(C);
%# average the 20 "slices" to get a 80-by-90-by-19 array
M = mean(M,4);
我是一名优秀的程序员,十分优秀!