How to detect jumps in a vector mod2pi.(x), when the elements of x are increasing

After integrating a toral differential equation I get as trajectory coordinates, the vectors x,y, and then calculate:
xt = mod2pi.(x)
yt = mod2pi.(y)
I want to insert a NaN between two consecutive elements of xt and yt, with the property that the left one is almost 2π, and the right one, almost 0.
How to detect such a jump?
Example:

x= [k*0.05  for k in 1:500]
xt=mod2pi.(v)

One trial:

n = 1
ix = findall(<(-π), diff(xt))
for i in ix
    insert!(xt, i + n, NaN)
    n += 1
end

I don’t quite understand your question, but I’ll take a shot. It’s not as efficient as possible, but I’m guessing this is for plotting so it that doesn’t matter.

function insertnanwheredecreases!(z)
	i = firstindex(z)+1
	while checkbounds(Bool, z, i)
		if z[i-1] > z[i] # decrease from index i-1 to i
			insert!(z, i, NaN)
			i += 1 # skip the new NaN
		end
		i += 1
	end
	return z
end

insertnanwheredecreases!(xt)

If you want some behavior based on both xt and yt then you’ll need to modify this function a bit. I might have missed some corner cases in what I wrote.

1 Like