Question regarding matrix manipulation

Hi all,
I have a (50x1) Array A2 which is a series of uneven sequential numbers (serial times), And a (50x1) array B2 of numbers (prices corresponding to prices).
A2 is our X axis and B2 is our y axis.
Now I have a new X axis X2 which is of size (99x1).
I want to fit the prices into the new 99x1 axis, so for 50 we have the corresponding values but for the 49 missing we want to fill in the value using neirought neibour but rather than looking above and below, it will just use above.

Answers (1)

Honglei Chen
Honglei Chen on 28 Jun 2012
Edited: Honglei Chen on 28 Jun 2012
Y2 = interp1(X1Y1X2'nearest')

6 Comments

I need it so that it chooses the nearest neighbour above the missing entry (rather than nearest neighbour above and below)
Even when I do run the code I get the following error:
Error using interp1 (line 257)
The values of X should be distinct.
I don't quite understand your first comment. For the second comment, it means that you have one x corresponding to two y values. According to your description, that means that at one instant, you have two prices corresponding to it. Is this really what you have as data?
Hi, discussing my above question comment. Nearest neihhbour finds the nearest value above and below. However I do not want to look below , just above, as it is prices the price would remain constant. Do you understand?
The reason why I get this error is that for my x axis, as is it is a financial time series there are some times which have two prices at the same time.....is there a way around this?
Are you saying if you have two points (x=1,y=3) and (x=2,y=4) and when you want to calculate the value at x=1.2, you want to return 4? Then this is not really nearest neighbor, you may need to do this on your own.
As to the second issue, you will have to do some pre-processing. Otherwise it's not a function and the interpolation won't work. For example, you can use unique() to keep either the lower or higher value.

Sign in to comment.

Categories

Find more on Interpolation in Help Center and File Exchange

Asked:

on 28 Jun 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!