Moving Median Absolute Deviation

The function which is used for calculating moving median absolute deviation is not available in matlab built-in functions. so this function may be useful for those which do not have matlab 2016 or later version. this simple function does the calculation for centered moving median absolute deviation. It do not skip the nan values. it is usable for the data with no missing values.  however it could be easily modified to handle missing value.

function [xmad,xmedian] = movmad(x, k)
xmad = zeros(length(x),1);
xmedian = zeros(length(x),1);
wz = (k - 1) / 2;
for i = 1:length(x)
 ks = i - wz;
 ke = i + wz;
 if ks < 1
 ks = 1;
 if ke > length(x)
 ke = length(x);
 xmad(i,1) = median(abs(x(ks:ke) - median(x(ks:ke))));
 xmedian(i,1) = median(x(ks:ke));

Leave a Reply

Your email address will not be published. Required fields are marked *