Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用MATLAB计算不等列单元的平均值_Matlab_Cell_Average - Fatal编程技术网

用MATLAB计算不等列单元的平均值

用MATLAB计算不等列单元的平均值,matlab,cell,average,Matlab,Cell,Average,我有一个具有不同列的单元格,如下所示: M= 我希望得到一个像Meam一样的平均值,1。 可以用MATLAB来做吗 谢谢如果要表示所有值,可以运行以下命令: mean(cell2mat(M)) cell2mat将M转换为一个维数为1xn的矩阵,其中n是M中所有值的个数,然后均值函数得到所有值的平均值 此外,如果要获取每个单元格的平均值,可以运行以下操作: cellfun(@mean,M) m = max(cellfun(@length,M)); // get the max length o

我有一个具有不同列的单元格,如下所示:

M=

我希望得到一个像Meam一样的平均值,1。 可以用MATLAB来做吗


谢谢

如果要表示所有值,可以运行以下命令:

mean(cell2mat(M))
cell2mat将M转换为一个维数为1xn的矩阵,其中n是M中所有值的个数,然后均值函数得到所有值的平均值

此外,如果要获取每个单元格的平均值,可以运行以下操作:

cellfun(@mean,M)
m = max(cellfun(@length,M)); // get the max length of the matrices
for i = 1 : length(M)
    M{i}(m+1) = 0
end
// mean of each column
means = mean(reshape(cell2mat(M), length(M), m + 1));
% in octav: 
% means = mean(reshape(cell2mat(M), m + 1, length(M))');
means = means(1:m);
这将得到每个单元格中每个矩阵的平均值

使现代化 由于列的大小不同,为了获得每列的平均值,我们可以按如下方式进行操作:

cellfun(@mean,M)
m = max(cellfun(@length,M)); // get the max length of the matrices
for i = 1 : length(M)
    M{i}(m+1) = 0
end
// mean of each column
means = mean(reshape(cell2mat(M), length(M), m + 1));
% in octav: 
% means = mean(reshape(cell2mat(M), m + 1, length(M))');
means = means(1:m);

有关更多详细信息,首先我们将所有数组调整为相同大小。然后,使用cell2mat将它们合并成一个矩阵,并删除为方便计算而添加的额外列。

谢谢您的回答。我怀疑我是否可以使用meancell2matM,因为我需要每个单元格都有相同的列,如果我这样做,它会给我被连接的矩阵的错误维数不一致,我想得到所有第一列的平均值,依此类推,直到最后,您提供给我的解决方案只计算每个单元格的平均值。我为一个简单的exmaple运行您的代码,我希望前四个单元格的平均值分别为2 3 4 5。但它不起作用:v1=[2 3 4 5];v2=[2 3 4 5 6 7];v3=[2 3 4 5 6 7];M={v1,v2,v3}M=maxcellfun@length,M;%%获取i=1时矩阵的最大长度:lengthM M{i}M+1=0 end%//每列的平均值means=meansureformecell2matm,lengthM,M+1;平均数=平均数1:m;结果:3.0000 1.6667 1.6667 5.0000 3.0000 4.0000..对我来说,代码输出不是我期望的。@Julliette我想你使用的是octav。无论如何,我已经更新了我的解决方案。您应该将平均值替换为:平均值=meanReformateCell2Matm,m+1,lengthM';如果使用副本中的解决方案将值放入数组,则可以使用nanmeanM,1获得平均值。