You could for example enforce that the matrix is already allocated, and then overwrite the old elements in every iteration