如何计算单元格数组的加权平均值?

编程入门 行业动态 更新时间:2024-10-24 23:29:30
本文介绍了如何计算单元格数组的加权平均值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

概括我之前的问题,如何对单元元素(即并应保留为数组的元素)进行加权平均)执行?

我首先要修改 gnovice的答案是这样的:

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 = sum(M.*weigth,dim+1)./sum(weigth,dim+1); %# Get the weighted mean across arrays

在此之前,请确保weight具有正确的形状.我认为需要注意的三种情况是

  • weight = 1(或任何常数)=>返回通常的平均值
  • numel(weight)== length(c)=>重量是每个单元元素c {n}(但对于固定n的每个数组元素都相等)
  • numel(weight)== numel(cell2mat(c))=>每个数组元素都有自己的权重...
  • 第一种情况很简单,第3种情况不太可能发生,所以目前我对第2种情况感兴趣:如何将权重转换为一个数组,以使M.*weight在上述总和中具有正确的维数?当然,也可以给出显示获得加权平均值的另一种方法的答案.

    编辑实际上,如果权重与c的结构相同,则情况3比情况1更为琐碎.

    这是我对情况2的意思的一个例子.

    c = { [1 2 3; 1 2 3], [4 8 3; 4 2 6] }; weight = [ 2, 1 ];

    应该返回

    meanArray = [ 2 4 3; 2 2 4 ]

    (例如,第一个元素(2 * 1 + 1 * 4)/(2 + 1)= 2)

    解决方案

    熟悉 REPMAT ,现在这是我的解决方案:

    function meanArray = cellMean(c, weight) % meanArray = cellMean(c, [weight=1]) % mean over the elements of a cell c, keeping matrix structures of cell % elements etc. Use weight if given. % based on stackoverflow/q/5197692/321973, courtesy of gnovice % (stackoverflow/users/52738/gnovice) % extended to weighted averaging by Tobias Kienzler % (see also stackoverflow/q/5231406/321973) dim = ndims(c{1}); %# Get the number of dimensions for your arrays if ~exist('weight', 'var') || isempty(weight); weight = 1; end; eins = ones(size(c{1})); % that is german for "one", creative, I know... if ~iscell(weight) % ignore length if all elements are equal, this is case 1 if isequal(weight./max(weight(:)), ones(size(weight))) weight = repmat(eins, [size(eins)>0 length(c)]); elseif isequal(numel(weight), length(c)) % case 2: per cell-array weigth weight = repmat(shiftdim(weight, -3), [size(eins) 1]); else error(['Weird weight dimensions: ' num2str(size(weight))]); end else % case 3, insert some dimension check here if you want weight = cat(dim+1,weight{:}); end; M = cat(dim+1,c{:}); %# Convert to a (dim+1)-dimensional matrix sumc = sum(M.*weight,dim+1); sumw = sum(weight,dim+1); meanArray = sumc./sumw; %# Get the weighted mean across arrays

    In generalisation of my previous question, how can a weighted average over cell elements (that are and shall remain arrays themselves) be performed?

    I'd start by modifying gnovice's answer like this:

    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 = sum(M.*weigth,dim+1)./sum(weigth,dim+1); %# Get the weighted mean across arrays

    And before that make sure weight has the correct shape. The three cases that I think need to be taken care of are

  • weight = 1 (or any constant) => return the usual mean value
  • numel(weight) == length(c) => weight is per cell-element c{n} (but equal for each array element for fixed n)
  • numel(weight) == numel(cell2mat(c)) => each array-element has its own weight...
  • Case one is easy, and case 3 unlikely to happen so at the moment I'm interested in case 2: How can I transform weight into a array such that M.*weight has the correct dimensions in the sum above? Of course an answer that shows another way to obtain a weighted averaged is appreciated as well.

    edit In fact, case 3 is even more trivial(what a tautology, apologies) than case 1 if weight has the same structure as c.

    Here's an example of what I mean for case 2:

    c = { [1 2 3; 1 2 3], [4 8 3; 4 2 6] }; weight = [ 2, 1 ];

    should return

    meanArray = [ 2 4 3; 2 2 4 ]

    (e.g. for the first element (2*1 + 1*4)/(2+1) = 2)

    解决方案

    After familiarizing myself with REPMAT, now here's my solution:

    function meanArray = cellMean(c, weight) % meanArray = cellMean(c, [weight=1]) % mean over the elements of a cell c, keeping matrix structures of cell % elements etc. Use weight if given. % based on stackoverflow/q/5197692/321973, courtesy of gnovice % (stackoverflow/users/52738/gnovice) % extended to weighted averaging by Tobias Kienzler % (see also stackoverflow/q/5231406/321973) dim = ndims(c{1}); %# Get the number of dimensions for your arrays if ~exist('weight', 'var') || isempty(weight); weight = 1; end; eins = ones(size(c{1})); % that is german for "one", creative, I know... if ~iscell(weight) % ignore length if all elements are equal, this is case 1 if isequal(weight./max(weight(:)), ones(size(weight))) weight = repmat(eins, [size(eins)>0 length(c)]); elseif isequal(numel(weight), length(c)) % case 2: per cell-array weigth weight = repmat(shiftdim(weight, -3), [size(eins) 1]); else error(['Weird weight dimensions: ' num2str(size(weight))]); end else % case 3, insert some dimension check here if you want weight = cat(dim+1,weight{:}); end; M = cat(dim+1,c{:}); %# Convert to a (dim+1)-dimensional matrix sumc = sum(M.*weight,dim+1); sumw = sum(weight,dim+1); meanArray = sumc./sumw; %# Get the weighted mean across arrays

    更多推荐

    如何计算单元格数组的加权平均值?

    本文发布于:2023-11-22 13:13:10,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1617552.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:平均值   数组   单元格

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!