MATLAB: Using two vectors to scale a vector

MATLABmatrix array

Hi,
I have three vectors of different scale, lets say
x1= (0:0.1:1)';
x2 = (0:1:10)';
x3 = [-0.01;0.99;2.01;2.98;3.99;5.001;5.99;7.021;8.001;8.999;10.11];
If I would like to scale x2 to x1, I can use interp1 function as
scaledx2 = interp1([min(x2),max(x2)],[min(x1),max(x1)],x2);
However, for x3 which is slightly different and has got some random noise on it. If I use the same interp1 function as
scaledx3 = interp1([min(x2),max(x2)],[min(x1),max(x1)],x3)
It understandbly gives me NAN values for the 1st and the last values which is incorrect. So how can I scale x3, based on the scales of x1 and x2?
Many Thanks

Best Answer

The NaN values at the ends are because interp1 must extrapolate and needs to be told how to do it.
Try this:
scaledx3 = interp1([min(x2),max(x2)],[min(x1),max(x1)],x3, 'linear','extrap')
I use 'linear' here, although any method will work.