nancumsum: Cumulative sum of a matrix, with user-specified treatment of NaNs.
Computes the cumulative sum of matrix A along dimension DIM, allowing the user to replace NaNs with zeros, to skip over them, or to reset on NaNs, maintaining NaNs as placeholders.
USAGE: B = nancumsum(A, DIM, NMODE)
A: Input matrix.
B: Output cumulative sum matrix, treating NaNs as determined by nmode.
DIM: B = nancumsum(A, DIM) returns the nan-cumulative sum of the elements
along the dimension of A specified by scalar DIM. For example,nancumsum(A,1) works down the columns, nancumsum(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 zeros (default).
2: MAINTAIN NaNs as position holders in B. (Skip NaNs without reset.)
3: RESET sum on NaNs, replacing NaNs with zeros.
4: RESET sum on NaNs, maintaining NaNs as position holders.
See also: cumsum, nansum, nancumprod, nanmean, nanmedian, ...
(nancumprod is available from the FEX. Other nan* may require Toolboxes)
Brett Shoelson (2020). nancumsum (https://www.mathworks.com/matlabcentral/fileexchange/14895-nancumsum), MATLAB Central File Exchange. Retrieved .
Came here via the pick-of-the-week blog post (https://blogs.mathworks.com/pick/2017/08/25/summing-and-multiplying-nans-use-cases/), and I thought I'd weigh in on my use case. I have a large image and an associated mask. I want to find the largest rectangle that fits within the mask (https://stackoverflow.com/questions/7770945/largest-rectangular-sub-matrix-with-the-same-number/7773870#7773870). This requires modes 3 and 4 in this submission, neither of which are available in the built-in cumsum function (as of R2018a). There are other similar times when I've wanted to find the longest run of 'true' values within a vector or along columns of a matrix. Using NaNs as reset markers makes this very convenient. This function allows me to do this.
excellent function, thanks brett
@Brett- Sorry it has taken so long. I just tried the download again, and still no .m file.
@Briana...Interesting. I just downloaded it and verified that the zip file does in fact contain the nancumsum.m file. Can you try to download again, and let me know if you get the same results?
I am having trouble downloading the function script. It seems the only file included in the .zip archive is the license agreement. Has anyone else had this problem?
Really helpful, thanks Brett.
Very useful. You may want to look at https://stackoverflow.com/q/12995399/2988730 for a suggested improvement to the reset cases that will eliminate the need for looping.
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.
As you should expect from Brett, excellent in all respects.
Fixes an issue with the vectorization of mode 3.
This submission fixes a bug pointed out by a couple of attentive users. Also, note that I have added new options for the treatment of NaNs, and eliminated one for special treatment of vectors. (Not necessary!)
Updated M file and license text file copyright to The MathWorks, Inc.
Fixing a bug (failed for scalar inputs).
Fixing bug in my code (isvector should have been isscalar) and in my examples(thanks, us). Also updating See also references.