Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4052404
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:18:57+00:00 2026-05-20T14:18:57+00:00

In generalisation of my previous question , how can a weighted average over cell

  • 0

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

  1. weight = 1 (or any constant) => return the usual mean value
  2. numel(weight) == length(c) => weight is per cell-element c{n} (but equal for each array element for fixed n)
  3. 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)

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T14:18:57+00:00Added an answer on May 20, 2026 at 2:18 pm

    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 http://stackoverflow.com/q/5197692/321973, courtesy of gnovice
    % (http://stackoverflow.com/users/52738/gnovice)
    % extended to weighted averaging by Tobias Kienzler
    % (see also http://stackoverflow.com/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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a generalization of my previous question about pluralize filter: Does lift have
I am creating a database of structures that can be of different types. Each
I would like to know how it is possible that C# Invoke function can
Yesterday I read a problem which can be translated into the following problem with
I have basic generalisation schema in database, I have superclass vehicle, and its subclases
Reading here , it seems modelling a custom exception class using a generalisation is
I have an iterator that returns context managers. I want a pythonic with statement,
I am trying to figure out a regular expression which matches any string that
How can I concatenate a class onto another class in this manner? class Person
I have a simple report sending framework that basically does the following things: It

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.