matches matlab implementation answers