The final reshape should use size(a) and not size(A).. Thus the code function q = newfilter1 (A, npoints) % assumes A is a muliple of npoints long a = reshape (A, npoints, numel(A) / npoints); q = reshape (repmat (min (abs(A - repmat(mean (A, 1), npoints, 1)), 1), npoints, 1), size (a)); endfunction should be used instead. Untested as is the previous code. D.