I have a cell array c
of equal-sized arrays, i.e. size(c{n}) = [ m l ... ]
for any n
. How can I get the mean
values (averaging over the cell array index n
) for all array elements in one sweep? I thought about using cell2mat
and mean
but the former does not add another dimension but changes l
to l*n
. And looping manually of course takes like forever...
我有一个大小相等的数组c,即任何n的大小(c {n})= [m l ...]。如何在一次扫描中获得所有数组元素的平均值(对单元数组索引n求平均值)?我想过使用cell2mat和mean,但前者不会添加另一个维度,而是将l更改为l * n。手动循环当然需要永远......
6 个解决方案
#1
15
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
#2
5
If you have a Higher version of matlab, It can be done by 'cellfun' function. This can treat the cells with unequal size array.
如果您有更高版本的matlab,可以通过'cellfun'功能完成。这可以用不等大小的阵列处理细胞。
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
#3
2
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. You can then calculate the mean using the MEAN function with the dimension argument:
你走在正确的轨道上。使用CELL2MAT将您的单元格数组转换为数值数组,然后使用RESHAPE构建三维矩阵。然后,您可以使用带有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
#4
2
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. Then:
可以x是细胞。然后:
var_mean = cellfun(@mean, x, 'UniformOutput', false); %columnwise mean value
var_mean = cellfun(@(in) mean(in(:)), x); %% mean value of the total "subcell"
#5
1
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
#6
0
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}
#1
15
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
#2
5
If you have a Higher version of matlab, It can be done by 'cellfun' function. This can treat the cells with unequal size array.
如果您有更高版本的matlab,可以通过'cellfun'功能完成。这可以用不等大小的阵列处理细胞。
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
#3
2
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. You can then calculate the mean using the MEAN function with the dimension argument:
你走在正确的轨道上。使用CELL2MAT将您的单元格数组转换为数值数组,然后使用RESHAPE构建三维矩阵。然后,您可以使用带有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
#4
2
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. Then:
可以x是细胞。然后:
var_mean = cellfun(@mean, x, 'UniformOutput', false); %columnwise mean value
var_mean = cellfun(@(in) mean(in(:)), x); %% mean value of the total "subcell"
#5
1
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
#6
0
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}