function r = cumsum3d( size, matrix )
%CUMSUM3D Cumulative sum of 3D matrix packed in a 2D matrix.
%	B = CUMSUM3D( [M N P], A ) returns a M-by-N-by-P matrix B
%	containing the cumulative sum of A, also a M-by-N-by-P matrix.
%	Both B and A are stored in (M*N)-by-P matrices (see NDX3D).
%	The cumulative sum is computed along all 3 axes.
%
%	See also: NDX3D, ELEM3D, SIZE3D.

%    Joseph M. Winograd 6-93
%    Copyright (c) 1993 by The MathWorks, Inc.
%    $Revision: 1.3 $  $Date: 1993/10/21 12:59:32 $

M = size(1);
N = size(2);
P = size(3);

r = cumsum( reshape( cumsum( reshape( ...
        cumsum( reshape( full(matrix), M, N*P ) )', ...
        N, M*P ) )', P, M*N ) )';
