function [y,c]=ndiff(x,n) % NDIFF % [y,c] = ndiff(x,n) differentiates the function in x % (a column vector or matrix thereof) n times padding % the end with zeros. The mean values of each column % vector before differentiation are returned in the % matrix c. (Useful for reconstruction.) [i,j]=size(x); y=x; c=zeros(i,j); for m=1:n, c(m,:)=mean(y); y=diff(y); end y=[y;zeros(n,j)];