sr = sign(real(Z));
si = sign(imag(Z));
zerohorz_r = (sr(:,1:end-1) .* sr(:,2:end)) <= 0;
zerohorz_i = (si(:,1:end-1) .* si(:,2:end)) <= 0;
zerohorz = zerohorz_r & zerohorz_i;
zerovert_r = (sr(1:end-1,:) .* sr(2:end,:)) <= 0;
zerovert_i = (si(1:end-1,:) .* si(2:end,:)) <= 0;
zerovert = zerovert_r & zerovert_i;
zerohorz(J,K) is true if in looking at Z(J,K) compared to Z(J,K+1), the real and imaginary components both changed sign, or one changed sign and the other is 0, or both are zero. If, for example, the real component changed sign from positive to negative, but the imaginary component did not change sign, then that is not considered to be a zero crossing.
zerovert(J,K) is the corresponding test, but for Z(J,K) compared to Z(J+1,K)
Note that these matrices are one column narrower or one row shorter than the original, since they deal with zero crossing in-between points.
The logic here:
- if the signs of two items are both +1 or both -1, then their product is +1, and this corresponds to the case where no zero crossing was made.
- if the signs of two items are -1 and +1, or +1 and -1, then their product is -1, and this corresponds to the case where a zero crossing was definitely made.
- if the signs of two items are both 0, then their product is 0. No crossing was made, but they are both 0, and you are looking for the zeros, not for the crossings as such
- if the signs are +1 and 0, or -1 and 0, or 0 and +1, or 0 and -1, then their product is 0. No crossing was made, but at least one of them was a 0. If you had a sequence such as +1, 0, -1, then the code would detect mark both of them; it would also mark both sites for a +1, 0, +1. You might want to refine which "direction" the zero needs to be approached from for your purposes.
Best Answer