%% Sum of Array Slices
% Use a vector dimension argument to operate on specific slices of an array.
% Create a 3-D array whose elements are 1.

A = ones(4,3,2);

% To sum all elements in each page of A, specify the dimensions in which to sum (row and column) using a vector dimension argument. Since both pages are a 4-by-3 matrix of ones, the sum of each page is 12.

S1 = sum(A,[1 2])

% If you slice A along the first dimension, you can sum the elements of the resulting 4 pages, which are each 3-by-2 matrices.

S2 = sum(A,[2 3])

% Slicing along the second dimension, each page sum is over a 4-by-2 matrix.

S3 = sum(A,[1 3])

% Starting in R2018b, to sum over all dimensions of an array, you can either specify each dimension in the vector dimension argument, or use the 'all' option.

S4 = sum(A,[1 2 3])
Sall = sum(A,'all')

% Copyright 2018 The MathWorks, Inc.