Matlab code for RS