MATLAB: Definite integral using cumtrapz()

integrationnumerical integration

I have a matrix of data points "Y" and want to numerically integrate each ith row of matrix Y from 0 to the ith element of vector "lim" which contains the upper limits of the integral. For example,
Y = 1 2 4 6
2 4 5 7
4 5 6 8
lim = [3 2 1]
The cumtrapz function uses unit spacing so I think the columns of Y represent the x-axis from x=0 to x=4. I would like to integrate the first row of Y from x=0 to x=3, the 2nd row from x=0 to x=2 and the 3rd row from x=0 to x=1. Is this possible to do using cumtrapz()? I'm not sure how to specify the limits.

Best Answer

I’m not certain what result you want.
Here are two possibilities, the first using trapz, the second using cumtrapz:
Y = [1 2 4 6
2 4 5 7
4 5 6 8];
lim = [3 2 1];
for k1 = 1:numel(lim)
Yint(k1,:) = trapz(Y(k1,1:lim(k1)+1));
end
for k1 = 1:numel(lim)
Yintc{k1,:} = cumtrapz(Y(k1,1:lim(k1)+1));
end
A loop for each seems to be the only option.