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

**URL:** <https://discourse.julialang.org/t/how-to-detect-jumps-in-a-vector-mod2pi-x-when-the-elements-of-x-are-increasing/96475>\
**Category:** General Usage\
**Created:** [March 22, 2023, 8:40pm UTC](https://discourse.julialang.org/t/how-to-detect-jumps-in-a-vector-mod2pi-x-when-the-elements-of-x-are-increasing/96475 "2023-03-22T20:40:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [March 22, 2023, 8:40pm UTC](https://discourse.julialang.org/t/how-to-detect-jumps-in-a-vector-mod2pi-x-when-the-elements-of-x-are-increasing/96475/1 "2023-03-22T20:40:27Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [March 22, 2023, 8:51pm UTC](https://discourse.julialang.org/t/how-to-detect-jumps-in-a-vector-mod2pi-x-when-the-elements-of-x-are-increasing/96475/2 "2023-03-22T20:51:40Z")

</div>

One trial:

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

```

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [March 22, 2023, 9:15pm UTC](https://discourse.julialang.org/t/how-to-detect-jumps-in-a-vector-mod2pi-x-when-the-elements-of-x-are-increasing/96475/3 "2023-03-22T21:15:34Z")

</div>

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.

```julia
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.
