example of how it might be done in matlab