An easier way to do this, using matlab