The most effective way to use interp1 command in matlab with two columns

3 次查看(过去 30 天)
I have two columns (same size) that I have to interpolate in Matlab. First column is a vector of time in hours (9.5, 9.6 9.73 10.13 etc...) and the other column are stock prices associated to each element of the column "time in hours". I also have third column with daily dates.
I wish to have some prices that are associated with each 15min, 30min, 45min, and 1hour (60min) that are missing in my price vector. And therefore, thee elements (15,30,45min and 60min) are also missing in my column of time per hours. I intend to use this command:
interp_prices = interp1(time, prices, 0:0.25:max(time));
But can I run the interp1 command by day? By that I mean, in addition to my column time and prices I have days and I don't want to interpolate between days... that is between say 16.5hours (calculated from midnight) to 9hours (am). Thanks!

采纳的回答

Charles Martineau
Charles Martineau 2012-6-11
I solved my problem this way:
% This datenum vector matches A. I'm assuming they're already sorted by date and time
At = datenum(num2str(A_dates), 'yyyymmdd') + datenum(0, 0, 0, A_hours, 0, 0);
incr = datenum(0, 0, 0, 0.25, 0, 0); % 0.25 hour
t = (At(1):incr:At(end)).'; % Full timespan of dataset, in 0.25 hour increments
frac_hours = 24*(t - floor(t)); % Fractional hours into the day
t_business_day = t((frac_hours > 9.4) & (frac_hours < 16.1)); % Time vector only where you want it
P = interp1(At, A_prices, t_business_day);

更多回答(2 个)

per isakson
per isakson 2012-6-7
Something like
new_times = 0:0.25:max(time);
day_hour = rem( new_times, 24 );
new_times( day_hour <= 8.9 | day_hour >= 16.6 ) = [];
interp_prices = interp1( time, prices, new_times );

Charles Martineau
Thanks Per - your code should work but the problem is that I just realized that sometimes for the same time say 9.25hrs I will have this time twice associated with two different prices. Hence X in interp1 is not unique... any suggestion?
  1 个评论
per isakson
per isakson 2012-6-7
The values time must be unique. Which price do you want to use, mean, max, min, first, last?
May three or more identical time values occur?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Time Series Objects 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by