Code covered by the BSD License  

Highlights from
wmean

4.5

4.5 | 2 ratings Rate this file 35 Downloads (last 30 days) File Size: 1.55 KB File ID: #14416

wmean

by Adam Auton

 

25 Mar 2007 (Updated 26 Mar 2007)

Weighted Mean or Average

| Watch this File

File Information
Description

For vectors, WMEAN(X,W) is the weighted mean value of the elements in X using non-negative weights W. For matrices, WMEAN(X,W) is a row vector containing the weighted mean value of each column. For N-D arrays, WMEAN(X,W) is the weighted mean value of the elements along the first non-singleton dimension of X.

Class support for inputs X and W:
   float: double, single

Example:
    x = rand(5,2);
    w = rand(5,2);
    wmean(x,w)

Acknowledgements
This submission has inspired the following:
Weighted Data Binning [wbin]
MATLAB release MATLAB 7.1.0 (R14SP3)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (2)
27 Mar 2007 John D'Errico

You posted this before, and I found a few problems in it then. All of those problems seems to have been resolved here. My thanks for cleaning this up and resubmitting.

This code has an H1 line. It has clean, readable help. It has error checks for all the errors I'd think to look for. It has internal comments, and it is vectorized.

If I could suggest one thing, I'd suggest that you add a few extra comment lines that include your name, the date the code was last modified, and your e-mail address (if you wish.) You might also note in the website description that this code should run with no problems in older releases of matlab.

11 May 2007 A B

Comments:
1. For input validation, use assert, or even better - use inputParser.
2. Find can be used with the 'first' or 'last' option.
3. Instead of sum(w.*x,dim), try dot(w,x,dim) as its more abstract.

Comments 1 and 2 above require recent versions of MATLAB.

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
statistics Adam Auton 22 Oct 2008 09:05:59
probability Adam Auton 22 Oct 2008 09:05:59
weighted mean or average Adam Auton 22 Oct 2008 09:05:59

Contact us at files@mathworks.com