Better implementation of eye(matrix) from #4164