Code covered by the BSD License  

Highlights from
nancumprod

5.0

5.0 | 1 rating Rate this file 0 Downloads (last 30 days) File Size: 2.41 KB File ID: #14927

nancumprod

by Brett Shoelson

 

08 May 2007 (Updated 05 Oct 2011)

Compute the cumulative product of matrices, specifying the treatment of NaNs.

| Watch this File

File Information
Description

nancumprod: Cumulative product of a matrix, with user-specified treatment of NaNs.

Computes the cumulative product of matrix A along dimension DIM, allowing the user to replace NaNs with ones, or to maintain NaNs as placeholders.
  
USAGE: B = nancumprod(A, DIM, NMODE)
 
ARGUMENTS:
 
A: Input matrix.
 
B: Output cumulative product matrix, treating NaNs as determined by nmode.
 
DIM: B = nancumprod(A, DIM) returns the nan-cumulative product of the elements along the dimension of A specified by scalar DIM. For example, nancumprod(A,1) works down the columns, nancumprod(A,2) works across the rows. If DIM is not specified, it defaults to the first non-singleton dimension of A.
 
NMODE: specifies how NaNs should be treated. Acceptable values are:
1: REPLACE NaNs with ones (default).
2: MAINTAIN NaNs as position holders in B. (Skip NaNs without reset.)
3: RESET product to 1 on NaNs, replacing NaNs with ones.
4: RESET product to 1 on NaNs, maintaining NaNs as position holders.
   
See also: cumprod, nancumprod, nansum, nanmean, nanmedian, ...
(nancumsum is available from the FEX; other nan* functions may require Toolboxes)

MATLAB release MATLAB 7.4 (R2007a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (7)
09 May 2007 Urs (us) Schwarz

a trouvaille of paramount importance to this community - offered by former CSSM senior brett shoelson; a function that should have been in ML stock for a long time.
needless to say that his clean coding fulfills all the criteria to be a five-star contender: concise h1, help text with meticulous examples, proper input and options error checking, and an interesting, vectorized engine.
us

09 May 2007 Urs (us) Schwarz

a trouvaille of paramount importance to this community - offered by former CSSM senior brett shoelson; a function that should have been in ML stock for a long time.
needless to say that his clean coding fulfills all the criteria to be a five-star contender: concise h1, help text with meticulous examples, proper input and options error checking, and an interesting, vectorized engine.
us

24 Mar 2011 Rick Patterson

I hope the author will not mind if I suggest what may be a better implementation of NMODE case 2 in the circumstances I mention above:

    case 2
        % DO NOT INCREMENT, BUT USE NaNs AS PLACEHOLDERS.
        B = A;
        B(B~=B) = 1;
        B = cumprod(B, dim);
        B(A~=A) = NaN;
    otherwise % case 3
...

24 Mar 2011 Rick Patterson

ie In the case where cumulative product of a matrix in the column dimension is required
eg in example 2) above, b=nancumprod(a,1,2)

04 Apr 2011 Brett Shoelson

Rick,
Thanks for the comment, though I'm not really sure I understand it. Are you suggesting that I do away with option 2 and force it to be treated like option 1?
 

20 Apr 2011 Rick Patterson

Brett,

Sorry for the garbled initial comment, my very first comment on the matter was lost to the ether. I'll attempt to explain further...

So using the setup of example 2:

a =

     8 NaN 6
     3 5 7
     4 9 2

and attempting to get a cumprod down columns with NaNs retained, via:

b=nancumprod(a,1,2)

yields:

b =

           8 NaN 25920
          24 480 181440
          96 4320 362880

ie the column products do not reset at the start of each column.

With the 'case 2' clause adjusted as per my post above, the result is instead:

b =

     8 NaN 6
    24 5 42
    96 45 84

which was a more reasonable treatment for the problem I had at hand.

26 Apr 2011 Brett Shoelson

Rick, I see now what you mean. I will circle back around to fix this case and re-post--as soon as I can get to it! :)
Thanks for the comments!
Brett

Please login to add a comment or rating.
Updates
09 May 2007

Fixing a bug that fails for scalar inputs.

14 May 2007

Replacing zero with one as identity element used for replacement in nmode option 1. (Thanks, Oren. Good catch.) Also (accordingly) changing default to nmode = 1, to match nancumsum behavior.

25 May 2007

Fixing a typo i the description. No code change.

04 May 2010

added copyright

21 Jun 2010

Updated M file and license text file copyright to The MathWorks, Inc.

29 Aug 2011

This submission fixes a bug found by a couple of attentive users. I have also added new options for treating NaNs, and eliminated (as unnecessary) special handling of vectors.

05 Oct 2011

Fixes an issue with the vectorization of mode 3.

Tag Activity for this File
Tag Applied By Date/Time
nan Brett Shoelson 22 Oct 2008 09:12:18
cumulative Brett Shoelson 22 Oct 2008 09:12:18
prod Brett Shoelson 22 Oct 2008 09:12:18
cumprod Brett Shoelson 22 Oct 2008 09:12:18

Contact us at files@mathworks.com