SUMM

Version 1.1.0.0 (1.83 KB) by Andy
Function analogous to DIFF, but takes sums instead.
375 Downloads
Updated 3 Sep 2009

View License

%SUMM Sum of consecutive pairs in an array, analogous to DIFF.
% SUMM(X), for a vector X, is [X(2)+X(1) X(3)+X(2) ... X(n)+X(n-1)].
% SUMM(X), for a matrix X, is the matrix of row sums,
% [X(2:n,:) + X(1:n-1,:)].
% SUMM(X), for an N-D array X, is the sum along the first
% non-singleton dimension of X.
% SUMM(X,N) is the N-th order sum along the first non-singleton
% dimension (denote it by DIM). If N >= size(X,DIM), SUMM takes
% successive sums along the next non-singleton dimension.
% SUMM(X,N,DIM) is the Nth sum function along dimension DIM.
% If N >= size(X,DIM), SUMM returns an empty array.
%
% Examples:
%
% If X = [3 7 5
% 0 9 2]
% then summ(X,1,1) is [3 16 7], summ(X,1,2) is [10 12
% 9 11],
% summ(X,2,2) is the 2nd order sum along the dimension 2, and
% summ(X,3,2) is the empty matrix.
%
% Note: There's probably a better way to do this. In testing, this
% is consistently three times slower than diff. But it was quick
% enough.
%
% This was inspired by the CSSM question:
% http://www.mathworks.com/matlabcentral/newsreader/view_thread/259947

Cite As

Andy (2024). SUMM (https://www.mathworks.com/matlabcentral/fileexchange/25208-summ), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2009a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Operators and Elementary Operations in Help Center and MATLAB Answers
Tags Add Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.1.0.0

Took into account several excellent suggestions by Jan Simon, including correct handling of empty matrices. Thanks!

1.0.0.0