如何对数组的数组进行平均?

Tob*_*ler 9 arrays matlab mean cell-array

我有一个c等大小数组的单元格数组,即size(c{n}) = [ m l ... ]任何数组n.如何在一次扫描中获取所有数组元素的mean值(对单元数组索引n求平均值)?我想过使用cell2matmean,但前者不另一个层面,但变化添加ll*n.手动循环当然需要永远......

gno*_*ice 17

如果所有数组的大小都相同,那么将它们存储在矩阵而不是单元格数组中会更有意义.这使得更容易在它们之间执行操作,比如采取平均值.您可以使用NDIMSCAT功能将数据转换为矩阵:

dim = ndims(c{1});          %# Get the number of dimensions for your arrays
M = cat(dim+1,c{:});        %# Convert to a (dim+1)-dimensional matrix
meanArray = mean(M,dim+1);  %# Get the mean across arrays
Run Code Online (Sandbox Code Playgroud)


Vic*_*Bai 5

如果您有更高版本的matlab,可以通过'cellfun'功能完成.这可以用不等大小的阵列处理细胞.

C = {1:10, [2; 4; 6], []};
Cmeans = cellfun(@mean, C)
Cmeans =
    5.5000    4.0000       NaN
Run Code Online (Sandbox Code Playgroud)

参考:https://groups.google.com/forum/?fromgroups =#!topic/comp.soft- sys.matlab / S_hyHxy11f0