The problem is the first line of the for loop
z <- z[k
Change z to any other letter and it executes. The trouble is that within the program, the first time the loop executes, it re-defines z as a scalar (equal to z[1 ). The second time through the loop it sees that z is a scalar (there is no z[2 within the scope of the program after the first loop) and hangs.
Peter