matlab code which implements replacement algorithm