%% Cumulatively Integrate Matrix with Nonuniform Spacing
% Cumulatively integrate the rows of a matrix where the data has a nonuniform spacing.
%
% Create a vector of x-coordinates and a matrix of observations that take place at the irregular intervals. The rows of Y represent velocity data, taken at the times contained in X, for three different trials.

X = [1 2.5 7 10];
Y = [5.2   7.7   9.6   13.2;
     4.8   7.0  10.5   14.5;
     4.9   6.5  10.2   13.8];

%
% Use cumtrapz to integrate each row independently and find the cumulative distance traveled in each trial. Since the data is not evaluated at constant intervals, specify X to indicate the spacing between the data points. Specify dim = 2 since the data is in the rows of Y.

Q1 = cumtrapz(X,Y,2)

% The result is a matrix of the same size as Y with the cumulative integral of each row.
% Copyright 2012 The MathWorks, Inc.