added mathworks solution.
generified the filterChain method.