This function performs cumulative mathematics on 2D matrices at arbitrary angles. This function works by using the Image Processing Toolbox function imrotate to rotate an input matrix to an arbitrary angle, then arithmatic is performed and the matrix is rotated back to its original position.
Assumptions: Matrix A represents data of equal spacing in x and y dimensions.
Issues: Sometimes you'll see some weird edge effects which corrupt a couple of rows and columns around the edges of the resulting matrix. This is the result of interpolating dummy padding values around the edges of the rotated matrix.
B = anglemath(A,theta,fun) B = anglemath(...,method)
B = anglemath(A,theta,fun) performs the function fun on 2D matrix A at angle theta. Angle theta is measured counterclockwise from the 3 o'clock position. Currently, anglemath only supports functions 'cumsum' and 'cumtrapz'. Support for more functions may be added in the future.
B = anglemath(...,method) specifies an interpolation method for image rotation. Method can be 'nearest', 'bilinear', or 'bicubic'. Default method is 'nearest'.
This function requires Matlab's Image Processing Toolbox.
Suppose you have a gridded dataset Z that looks like this:
% Create some data: x = 205:.3:342; y = 39:-.3:-60; [X,Y] = meshgrid(x,y); Z = sind(2*X)+cosd(2*Y)+0.2*rand(size(X)); Z(100:103,200:203) = 50; % <-this will help us identify direction of operation. % Display the data: imagesc(x,y,Z) axis image xy caxis([-1.5 2.2])
Above I set a few grid points to 50 to help us trace direction below. Using the anglemath function with theta set to 0 is equivalent to operating along dimension 2 of a matrix:
figure subplot(121) imagesc(x,y,cumsum(Z,2)) axis image xy title('cumsum(Z,2)','fontname','courier') caxis([-200 500]) subplot(122) imagesc(x,y,anglemath(Z,0,'cumsum')) axis image xy title('anglemath(Z,0,''cumsum'')','fontname','courier') caxis([-200 500])
Similar to above, using anglemath with theta set to -90 is equivalent to operating along dimension 1 of a matrix:
figure subplot(121) imagesc(x,y,cumtrapz(Z,1)) axis image xy title('cumtrapz(Z,1)','fontname','courier') caxis([-200 500]) subplot(122) imagesc(x,y,anglemath(Z,-90,'cumtrapz')) axis image xy title('anglemath(Z,-90,''cumtrapz'')','fontname','courier') caxis([-200 500])
But we didn't come here to do what inbuilt functions already do. Suppose you want to get the cumulative sum along Z at some arbitrary angle, say, 41.2345°. That's easy:
figure imagesc(x,y,anglemath(Z,41.2345,'cumsum')) axis image xy
Differences between interpolation methods are often imperceptible, but if it makes a difference to you, you can specify your favorite interpolation method:
figure imagesc(x,y,anglemath(Z,-123.4567,'cumsum','bilinear')) axis image xy
This function and supportind documentation were written by Chad A. Greene of the University of Texas at Austin's Institute for Geophysics (UTIG), September 2015.