Why is this code wrong?

That’s impossible to tell without an MWE really. But in principle it does sound like the sort of thing that simply running your code line-by-line and checking your output vector after every step should solve.