如何对数组的元胞数组求平均值?

发布于 2024-10-20 02:49:25 字数 283 浏览 0 评论 0原文

我有一个大小相等的数组的元胞数组 c,即任何 nsize(c{n}) = [ ml ... ] >。如何在一次扫描中获取所有数组元素的平均值值(对元胞数组索引n求平均值)?我考虑过使用 cell2matmean 但前者没有添加另一个维度,而是将 l 更改为 l*n 。手动循环当然需要永远......

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

梦里的微风 2024-10-27 02:49:25

如果所有数组的大小相同,则将它们存储在矩阵而不是元胞数组中更有意义。这使得在它们之间执行操作变得更容易,例如取平均值。您可以使用函数 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

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:

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
涙—继续流 2024-10-27 02:49:25

如果您有更高版本的 matlab,可以通过“cellfun”函数来完成。这可以处理具有不等大小数组的单元格。

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

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

If you have a Higher version of matlab, It can be done by 'cellfun' function. 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

幽梦紫曦~ 2024-10-27 02:49:25

你走在正确的轨道上。使用 CELL2MAT 将元胞数组转换为数值数组,然后 RESHAPE 构建三维矩阵。然后,您可以使用带有维度参数的 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

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:

>> 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
感情废物 2024-10-27 02:49:25

我在以下链接中找到了一种在元胞数组中查找平均值的简单方法:
http://www.gomatlab.de/cellfun-t25114.html

五月 x 是单元格。然后:

var_mean = cellfun(@mean, x, 'UniformOutput', false); %columnwise mean value


var_mean = cellfun(@(in) mean(in(:)), x); %% mean value of the total "subcell"

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

May x be the cell. Then:

var_mean = cellfun(@mean, x, 'UniformOutput', false); %columnwise mean value


var_mean = cellfun(@(in) mean(in(:)), x); %% mean value of the total "subcell"
百变从容 2024-10-27 02:49:25

这只是循环遍历单元格,意味着数组向下直到它成为一个单例。不需要那么长时间,这意味着 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

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.

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
何以笙箫默 2024-10-27 02:49:25

感谢您的其他评论,但有时,很难重新排列数据或更改数据的保存方式。对于遇到此问题的人,这里是解决方案,祝您愉快。

a=0;
MyCellAddFun=@(Input) a*eye(size(Input))+Input;
temp=arrayfun(@(ind) MyCellAddFun(CellData{ind}),1:length(CellData),'uniformoutput',false);
answer=temp{end}

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}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文