Add and remove element of vector at specific position
<p>Let <code>X = [1, 2, 3, 4, 5]</code> and <code>Y = [1, 2, 1, 0, 1]</code> be vectors where <code>X</code> maps into <code>Y</code>. 
Now I want to identify the maximum and minimum of <code>Y</code>, which is easy: <code>[value_min, id_min] = min(Y) = [0, 4]</code> and <code>[value_max, id_max] = max(Y) = [2, 2]</code>.
Then I want to remove the element from <code>X</code> corresponding to the minimum in <code>Y</code> and expand evenly around the element in <code>X</code> corresponding to the maximum in <code>Y</code>, while keeping the number of points equal. For this example we remove  <code>X(4)=[]</code>. Then we expand like <code>X(2)=(X(2) - X(1))/2</code> and <code>X(3)=(X(3) - X(2))/2</code> such that <code>X</code> looks like <code>X = [1, 1.5, 2.5, 3, 5]</code>. How can I achieve this? I think there is a general pattwern.</p>

<h2>Solution</h2>

<p>Now the following snipped should work for any vector of length <code>N</code>. Note that the first and final element are fixed.</p>

<pre><code>[value_max, id_max] = max(Y(2:N-1));
X(id_max) = (X(id_max) - X(id_max-1))/2;
X(id_max+1) = (X(id_max+1) - X(id_max))/2;
[value_min, id_min] = min(Y(2:N-1));
X(id_min)=[];
</code></pre>