The author seems to be using
badidx=find(abs(arg(S))>0);
to test if S is complex at any point in the array.
But if that is the case why not use isreal()?
While I am not a mind reader, one possibility is that he'd have to use something like
arrayfun(@isreal, x)
since isreal is not an elementwise operation. I'm not intimately familiar with isreal to know if there are any corner cases that this would prove problematic.
And I don't see how S could be complex from the code above?
S = S + r*S*dt + alphavec.* exp(r*(beta-1)*T).*S.^beta*rdt.*rndmat1(:,i);
Raises a number to the power beta, which has magnitude less than one, thus the result would be complex if an element of S is negative.