Just modify your loop a little bit:
for i = 3:length(nums)
if nums(i) == nums(i-1) && nums(i) == nums(i-2)
if nums(i) == 1
nums(i) = 0;
nums(i) = 1;
Oh, I see that you also require the same number of 1s and 0s. This doesn't ensure that. The easiest solution (though certainly not the fastest) is to add a check at the end that there are the same number; if not, reiterate until so.