Very similar to the standard integrate function, except that singleton dimensions are ignored.
As described in the integrate function description, integrating over dimensions with a single value (singleton dimensions) returns zero because the area under a single point is zero. In some cases, particularly when you are not sure which dimensions are singleton, this behavior can cause difficulties. The integrate2 function automatically ignores all dimensions with a size of one, which avoids the problem of a zero valued integrals due to singleton dimensions.
out = integrate2(A, 1, x1);
Integrates A over the first dimension in the matrix.
x1 is the corresponding position vector.
out = integrate2(A, d, x1, x2, ...);
Calculates the integral of A over the specified dimension(s) d.
d is a vector containing the dimensions over which to integrate.
xi is the position vector corresponding to the dimensions of A over which the integration is occurring. If any of the xi vectors only have 1 element, integrate returns 0.
In the following example, we compare the integrate and integrate2 commands when integrating over matrices with singleton dimensions.
# create 3D matrix of results: data(x,y,z) where
# there are 50 'x' sample points, 1 'y' sample points
# and 40 'z' sample points. This is typical of data
# from a 2D monitor oriented in the XZ plane.
data = X^2 + Z^2;
?integrate2(data, [1,2,3], x,y,z); # Integrate2 ignores singleton dimension, giving non-zero result.
?integrate (data, [1,2,3], x,y,z); # Result is zero because of the singleton dimension
?integrate (data, [1,3] , x,z ); # Get the same result as integrate2 by integrating over x and z, but not y.