[英]How to average over a cell-array of arrays?
I have a cell array c
of equal-sized arrays, ie size(c{n}) = [ ml ... ]
for any n
. 我有一个大小相等的数组
c
,即任何n
size(c{n}) = [ ml ... ]
。 How can I get the mean
values (averaging over the cell array index n
) for all array elements in one sweep? 如何在一次扫描中获得所有数组元素的
mean
(对单元数组索引n
求平均值)? I thought about using cell2mat
and mean
but the former does not add another dimension but changes l
to l*n
. 我想过使用
cell2mat
和mean
但前者不会添加另一个维度,而是将l
更改为l*n
。 And looping manually of course takes like forever... 手动循环当然需要永远......
If all of your arrays are the same size, it makes more sense to store them in a matrix rather than a cell array. 如果所有数组的大小都相同,那么将它们存储在矩阵而不是单元数组中会更有意义。 That makes it easier to perform operations across them, like taking the mean.
这使得更容易在它们之间执行操作,比如采取平均值。 You can convert your data to a matrix using the functions NDIMS and CAT :
您可以使用NDIMS和CAT功能将数据转换为矩阵:
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
If you have a Higher version of matlab, It can be done by 'cellfun' function. 如果您有更高版本的matlab,可以通过'cellfun'功能完成。 This can treat the cells with unequal size array.
这可以用不等大小的阵列处理细胞。
C = {1:10, [2; 4; 6], []};
Cmeans = cellfun(@mean, C)
Cmeans =
5.5000 4.0000 NaN
Reference: https://groups.google.com/forum/?fromgroups=#!topic/comp.soft-sys.matlab/S_hyHxy11f0 参考: https : //groups.google.com/forum/?fromgroups =#!topic / comp.soft- sys.matlab / S_hyHxy11f0
I found an easy way to find the mean values within a Cell array on the following link: http://www.gomatlab.de/cellfun-t25114.html 我找到了一种在以下链接上查找Cell数组中的平均值的简单方法: http : //www.gomatlab.de/cellfun-t25114.html
May x
be the cell. 可以
x
是细胞。 Then: 然后:
var_mean = cellfun(@mean, x, 'UniformOutput', false); %columnwise mean value
var_mean = cellfun(@(in) mean(in(:)), x); %% mean value of the total "subcell"
You're on the right track. 你走在正确的轨道上。 Use CELL2MAT to convert your cell array to a numerical array and then RESHAPE to construct a three dimensional matrix.
使用CELL2MAT将您的单元格数组转换为数值数组,然后使用RESHAPE构建三维矩阵。 You can then calculate the mean using the MEAN function with the dimension argument:
然后,您可以使用带有dimension参数的MEAN函数计算平均值:
>> c = {[1 2 3; 4 5 6] [7 8 9; 12 13 14]} c = [2x3 double] [2x3 double] >> mean(reshape(cell2mat(c), [2, 3, 2]), 3) ans = 4 5 6 8 9 10
This just loops through the cell and means the array down until it is a singleton. 这只是循环遍历单元格并意味着向下直到它是一个单例。 It doesn't take that long, this is 40 million floats being meaned, takes 1 second.
它不需要那么长时间,这是4000万个浮子被摧毁,需要1秒钟。
function n = big_mean
tic
c = cell(1000);
for ii = 1:length(c)
c{ii} = rand(8,7,6,5,4,3,2);
end
n = all_cells(c);
toc
end
function n = all_cells(c)
n = zeros(length(c),1);
for ii = 1:length(c)
n(ii) = cell_mean(c{ii});
end
n = mean(n);
end
function n = cell_mean(n)
while length(size(n))~=2
n = mean(n);
end
end
Elapsed time is 1.042459 seconds.
ans =
0.4999
thanks for your other comments, but sometimes, it is hard to rearrange the data or change the way they are saved. 感谢您的其他评论,但有时,很难重新排列数据或更改它们的保存方式。 For those of you who have this issue, here is the solution, Enjoy.
对于那些有这个问题的人来说,这就是解决方案,享受。
a=0;
MyCellAddFun=@(Input) a*eye(size(Input))+Input;
temp=arrayfun(@(ind) MyCellAddFun(CellData{ind}),1:length(CellData),'uniformoutput',false);
answer=temp{end}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.