You should sketch the two behaviors (don't use i or j as a variable name, and use comments to describe what your code is doing):
if Tin(t)<Ts
%temperature below setpoint, turn heating on
v(t)=1;
elseif v(t-1)==1 && Tin(t)>Tmax
%heating was on and max temp reached, turn off
v(t)=0;
else
%otherwise keep the same setting as last time interval
v(t)=v(t-1);
end