Calculation for absolute value pattern I have a weird pattern I have to calculate and I don't quite know how to describe it, so my apologies if this is a duplicate somewhere..
I want to solve this pattern mathematically. When I have an array of numbers, I need to calculate a secondary sequence (position) based on the index and the size, i.e.:
1 element:
index:    0
position: 1

2 elements:
index:    0  1
position: 2  1

3 elements:
index:    0  1  2
position: 2  1  3

4 elements:
index:    0  1  2  3
position: 4  2  1  3

5 elements:
index:    0  1  2  3  4
position: 4  2  1  3  5

6 elements: 
index:    0  1  2  3  4  5
position: 6  4  2  1  3  5

etc....
The array can be 1-based as well if that would make it easier. I wrote this out to 9 elements to try and find a pattern but the best I could make out was that it was some sort of absolute value function with a variable offset...

For an array with $n$ elements, the function is:
$$f(n,k)=1+2(k-[n/2])$$
when $k\geq [n/2]$ and
$$f(n,k)=2+2([n/2]-k-1)$$
when $k<[n/2]$, where $[\cdot]$ is the floor function. Example:
$$f(5,4)=1+2(4-2)=5$$.
$$f(5,1)=2+2(1-2+1)=2$$
