A Matrix Model for the Topological String I: Deriving the Matrix Model