MATLAB: Inserting Elements in middle of 1-D, 2-D, N-D Array

average value in middleinsert pointsrefine array

Dear All,
I have got an array X = [1 2 3 4 5].
Want to refine the points by averaging neighbours and insert in the middle.
X1 = [1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0].. So, 5 points have become 9 points.
Is there an easier way to do it for a general array?
I would like to make it work in N-dimension also … for refining the points.
Any help is much appreciated.
Thank You in advance,
K Vijay Anand.

Best Answer

interp1() is suitable for such cases.
X = [1 2 3 4 5];
n_new = 9;
X_new = interp1(linspace(0,1,numel(X)), X, linspace(0,1,n_new))
For a high-dimensional case, you need to specify whether you want to apply it on a single dimension or all the dimensions?