That initializes x(1) = 0
x(k) = x(k-1)+dxdt*Ts;
so when k = 2 (the first iteration), x(2) is going to be assigned. So after that x will be a vector rather than a scalar.
d3xdt3 = (gain*x-a1*y-a2*z-gain*(x.^3));
That uses all of x, so from k = 3 and later, d3xdt3 will be a vector because x became a vector by the end of k = 2
d3xdt3 is a vector, so the right hand side is a vector, but you are trying to assign the vector into the single location z(k)