Azzera filtri
Azzera filtri

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

3 visualizzazioni (ultimi 30 giorni)
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!

Risposta accettata

Charles Martineau
Charles Martineau il 11 Giu 2012
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);

Più risposte (2)

per isakson
per isakson il 7 Giu 2012
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
Charles Martineau il 7 Giu 2012
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 Commento
per isakson
per isakson il 7 Giu 2012
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?

Accedi per commentare.

Categorie

Scopri di più su Time Series Objects in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by